在JQuery Mobile中显示谷歌地图

Showing a Google Map in JQuery Mobile

本文关键字:显示 谷歌地图 Mobile JQuery      更新时间:2023-09-26

我正在尝试构建一个JQuery Mobile应用程序。我希望这个应用程序包含一个谷歌地图。我的实现是基于jquery ui地图插件。他们的示例代码可在http://jquery-ui-map.googlecode.com/svn/trunk/demos/jquery-google-maps-mobile.html#basic_map.

即使有了这个例子,我仍然无法得到一张地图。我觉得我在使用最基本的代码。有人能告诉我我做错了什么吗?我的代码如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/resources/scripts/jquery.ui.map.full.min.js" />
</head>
<body>
    <div id="result" data-role="page">
        <div data-role="header"><h1>My App</h1></div>
        <div data-role="content">
          <div id="resultMap" style="height:200px; width:200px; background-color:Lime;">   
          </div>
        </div>
    </div>
    <br /><br />
    <script type="text/javascript">
        $("#result").live("pageshow", function () {
            $("#resultMap").gmap();
        });
    </script>
</body>
</html>

好的。我是这样做的:

var home = new google.maps.LatLng(x, y);
$('#directions_map').live("pageshow", function() {
    $('#map_canvas_1').gmap('refresh');
    });
$('#directions_map').live("pagecreate", function() {
    $('#map_canvas_1').gmap({'center': home, 'zoom':17 });
    $('#map_canvas_1').gmap('addMarker', { 'position': home, 'animation' : google.maps.Animation.DROP } );
    });

所以请尝试:
-在pagecreate
上创建gmap-仅在pageshow上刷新
-标记是奖金;-)

让我知道这是否奏效。这是我的一个工作示例,所以应该没问题。我认为在pageshow之前设置gmap很重要。如果你认为JQM是一个舞台。。。在幕布拉开之前,就要进行一场演出了。也许对谷歌魔术来说太晚了。Pagecreate似乎更好。。。

你试过这个吗:

jquery移动与谷歌地图

我发现它很容易实现。

我第一次尝试时也无法从jQuery UI映射中获得基本示例。在处理了jquery-ui-map-3.0-rc目录的"demos"文件夹中的示例之后,我去掉了所有不必要的html和javascript,以显示一个非常基本的映射。不幸的是,该网站有一些代码片段,而不是一个完整的、有效的但极简主义的例子。

您必须检查对链接和脚本的引用是否正确。

<!doctype html>
<html lang="en">
   <head>
        <title>Example with Google maps and jQuery - Google maps jQuery plugin</title>
        <link type="text/css" rel="stylesheet" href="css/style.css" >
    </head>
    <body>
        <div id="map_canvas" class="map rounded"></div>     
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
        <script type="text/javascript" src="js/jquery-1.7.1/jquery.min.js"></script>
        <script type="text/javascript" src="../ui/jquery.ui.map.js"></script>
        <script type="text/javascript">
                    $('#map_canvas').gmap({'center': '57.7973333,12.0502107', 'zoom': 10, 'disableDefaultUI':true, 'callback': function() {
                        var self = this;
                        self.addMarker({'position': this.get('map').getCenter() }).click(function() {
                            self.openInfoWindow({ 'content': 'Hello World!' }, this);
                        }); 
                    }});
        </script> 
    </body>
</html>