2012-02-05 126 views
1

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

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

<!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> 
+0

map()而不是gmap()? – 2012-02-05 19:16:41

+0

:(不幸的是没有, – 2012-02-05 20:08:13

回答

1

好的。我做这样的:

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
刷新我知道这是否有诀窍。这是来自我的一个工作示例,所以它应该没问题。我认为在页面展示之前设置gmap非常重要。如果你认为JQM是一个舞台,那么在揭开舞台之前,页面展示才是正确的。 Google Magic可能来不及。页面创建似乎更好...

+0

没有运气。有没有限制,你不能在localhost下运行它?这太奇怪了。你有一个完整的.html文件可用吗?我只是不能相信这不会BTW:谢谢你的帮助 – 2012-02-06 17:26:17

+0

可能是本地主机,但我不是专家。请检查我从哪个页面复制上述内容:http://www.franckreich.de/5/about .html - 我将在稍后再次删除该链接 – frequent 2012-02-06 18:49:53

+0

经常感谢我下载了你的代码并从localhost运行了它有趣的是,它的工作原理!我将继续挖掘并试图找出为什么我的代码不工作。我很感激你的帮助,我会在这里发表另一条评论一次(如果)我找出为什么我的其他代码无法正常工作。 – 2012-02-07 16:46:56

0

你有没有尝试过这样的:

jquery-mobile with google maps

我发现它非常容易实现。

+0

是的,我看到了,这就是我的链接指向的地方,然而,我的代码不起作用,我无法弄清楚缺少的东西,我尝试过,没有任何运气重试。他们显示的示例在我的浏览器中工作。 – 2012-02-05 20:54:39

0

我也无法从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>