2012-11-02 33 views
1

我有一个div移动页面,用于构建应用程序。当我定义:JQuery Mobile无法将GPS坐标拖入Google地图

var latitude = position.coords.latitude; 
var longitude = position.coords.longitude; 

无论是在我的功能,甚至外面,然后尝试和使用它们:

latlng1 = new google.maps.LatLng(latitude, longitude); 

我的页面没有加载,并且只给了我的CSS边框等

但是,如果我只是删除(latitude, longitude);并将(40.716948, -74.003563); 纳入latlng1 = new google.maps.LatLng(40.716948, -74.003563);它的工作原理。

为什么定义的变量值不会传入LatLng?

这里是源的DIV部分:

<!-- My Location --> 
<div data-role="page" data-theme="b" id="my_loc" class="page-map1"> 

    <div data-role="header" data-theme="a"> 
    <a href="#home" data-icon="arrow-l">Back</a> 
    <h1>My Location</h1> 
    </div> 

    <div data-role="content"> 

    <h2>Map</h2> 

    <div id="map1" class="gmap"></div> 

     <script type="text/javascript"> 
     var map1, latlng1, options1, latitude, longitude; 

     //these lat and long variable definitions are messing me up !! 
     //latitude = position.coords.latitude; 
     //longitude = position.coords.longitude; 

     function initialize() { 
      latlng1 = new google.maps.LatLng(latitude, longitude); 
      //40.716948, -74.003563); 
      //Lat, Lon); 
      //(position.coords.latitude, position.coords.longitude); 
      //40.716948, -74.003563); change from new jersey 
      options1 = { zoom: 14, center: latlng1, mapTypeId:google.maps.MapTypeId.ROADMAP }; 
      map1 = new google.maps.Map(document.getElementById("map1"), options1); 
     } 

     $('.page-map1').live("pagecreate", function() { 
      initialize(); 
     }); 

     $('.page-map1').live('pageshow',function(){ 
      console.log("test"); 
      google.maps.event.trigger(map1, 'resize'); 
      map1.setOptions(options1); 
     }); 
     </script> 

    </div> 
    </div> 


    <div data-role="page" id="map" data-theme="a"> 
    <div data-role="header" data-position="fixed"> 
     <h1>Page Header</h1> 
    </div> 
    <div data-role="content"> 
     <p class="errorMsg">Your location could not be determined.</p> 
     <div id="gMap"></div> 
    </div> 
    <div data-role="footer" data-position="fixed"> 
     <h4>TRACK</h4> 
    </div> 
    </div> 
    <!-- /My Location --> 
</div> 
+1

你可以修复'输入代码在这里吗? –

+0

我不是一个jquery的家伙,也不了解你想要做的事情,但它确实看起来像你设置'纬度'和'经度'的值被注释掉了。 –

回答

0

显然不填充您的经度和纬度的变量。为了让它们被填充,当然你需要首先调用navigator.geolocation.getCurrentPosition方法?但即使在该阶段之前,您也希望确保浏览器支持地理位置(请检查navigator.geolocation布尔值)。

检出this tutorial并使您的逻辑更像这样。一旦你做完了,alert/console.log你的经度和纬度变量,以确保它们实际上被填充。