2011-08-30 60 views
1

向您展示我正在尝试使用工作版本所做的工作的最简单方法:http://jsfiddle.net/t8Brm/11/具有GEO位置支持的所需现代浏览器。从navigator.geolocation.getCurrentPosition内部调用此工具

在JS代码中,我已经注释掉了导致问题的代码,以便您可以先看到它的工作原理。

的问题是: 我怎么能取消下面的代码使用地理位置添加标记,并在地图中心,没有得到Error: position.coords is undefined

$(this).trigger("gMap.addMarker", { 
    latitude: latitude, 
    longitude: longitude, 
    content: "You Are Here" 
}); 
$(this).trigger("gMap.centerAt", { 
    latitude: latitude, 
    longitude: longitude 
}); 

这是原来的插件:http://github.com/marioestrada/jQuery-gMap我有增加了GEO位置的额外功能。如下图所示:

function generateDirections(from, to, directionHtml){ 
      var directionsDisplay; 
      var directionsService = new google.maps.DirectionsService(); 

      $('#'+directionHtml).html(''); 

      var request = { 
       origin: from, 
       destination: to, 
       travelMode: google.maps.DirectionsTravelMode.DRIVING 
      }; 

      directionsDisplay = new google.maps.DirectionsRenderer(); 
      directionsDisplay.setMap($gmap); 
      directionsDisplay.setPanel(document.getElementById(directionHtml)); 
      directionsService.route(request, function(response, status){ 
       if(status == google.maps.DirectionsStatus.OK){ 
        directionsDisplay.setDirections(response); 
        if($("#gmaps_from").val().length === 0) 
         $("#gmaps_from").val(response.routes[0].legs[0].start_address); 
       } 
      }); 

      setTimeout(function(){ 
       $("#"+directionHtml).slideDown("slow"); 
      },1000); 
     } 

     //make this available to the click element 
     $.data($('#gmaps_getdirections')[0], 'inst', this); 

     $('#gmaps_getdirections').click(function(e) { 
      e.preventDefault(); 

      var from = $('#gmaps_from').val(); 
      if(opts.address){ 
       var to = opts.address; 
      } else { 
       var to = parseFloat(opts.latitude) + ", " + parseFloat(opts.longitude); 
      } 

      generateDirections(from, to, "gmaps_directions"); 

      //open the google window 
      //window.open("http://maps.google.com/maps?saddr=" + from + "&daddr=" + to, "GoogleWin", "menubar=1,resizable=1,scrollbars=1,width=750,height=500,left=10,top=10"); 
     }); 

     if(opts.geolocation && navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position){ 
       var latitude = position.coords.latitude; 
       var longitude = position.coords.longitude; 

       var from = parseFloat(latitude) + ", " + parseFloat(longitude); 

       if(opts.address){ 
        var to = opts.address; 
       } else { 
        var to = parseFloat(opts.latitude) + ", " + parseFloat(opts.longitude); 
       } 

       /*$(this).trigger("gMap.addMarker", { 
        latitude: latitude, 
        longitude: longitude, 
        content: "You Are Here" 
       }); 
       $(this).trigger("gMap.centerAt", { 
        latitude: latitude, 
        longitude: longitude 
       });*/ 

       generateDirections(from, to, "gmaps_directions");      
      }); 
     } 
+0

你能将你的帖子形成一个问题吗?在jsFiddle链接之后没有更多的启发... – Matt

+0

“在JS代码中,我已经注释了导致问题的代码:”毫无疑问,因为我知道是什么导致了问题。 –

+1

那你为什么把它发布到SO? – Matt

回答

0

你可以执行像这样的等价的JavaScript API V3代码:

new google.maps.Marker({position:new google.maps.LatLng(latitude,longitude), 
    map:$gmap, 
    title:"You Are Here"}); 
$gmap.setCenter(new google.maps.LatLng(latitude,longitude)); 

中的jsfiddle与此更换您的注释掉的代码都会在地图上的标记并一度集中在你的位置在地图上。

看起来,其他位置的中心在此之后在某个点被触发。 (我假设你可以照顾关闭它,但如果没有,只是在评论中这样说,我或其他人将毫无疑问地看起来更多。)

0

确定 - 在FF测试 - 这里有一个我发现了什么 - $(this).trigger("gMap.addMarker", {...使你的函数进行再次评估。第一次找到lat/long,但在触发器调用后,再次调用同一个函数,并再次指向该函数。

我不熟悉插件,但我试图触发元素(#google_maps):http://jsfiddle.net/t8Brm/18

这修复了递归调用问题,但引发了另一个异常指定了无效或非法的字符串。

希望这点能指引您朝着正确的方向发展。