2013-05-17 66 views
1

我一直在尝试使用地图V3,以获得用户的位置,并显示一个固定的目标从那里位置的路径来获取用户的位置。但我没有找到一个解决方案来获取用户位置,我已经阅读了API,但找不到任何东西检查了所有的例子仍然是一样的。希望有人能帮助我Thanx。如何使用谷歌地图V3

回答

2

解决方案:

<script type="text/javascript" src="http://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
function initialize() { 
    var loc = {}; 
    var geocoder = new google.maps.Geocoder(); 
    if(google.loader.ClientLocation) { 
     loc.lat = google.loader.ClientLocation.latitude; 
     loc.lng = google.loader.ClientLocation.longitude; 

     var latlng = new google.maps.LatLng(loc.lat, loc.lng); 
     geocoder.geocode({'latLng': latlng}, function(results, status) { 
      if(status == google.maps.GeocoderStatus.OK) { 
       alert(results[0]['formatted_address']); 
      }; 
     }); 
    } 
} 

google.load("maps", "3.x", {other_params: "sensor=false", callback:initialize}); 
+0

thanx的答复,但我不能得到这个工作,你:(能帮助我,请...林新来的谷歌地图。 –

0

google.loader.ClientLocation只给出了一个非常粗略估计为用户的位置。

如果HTML5地理位置不存在或不被用户允许,最佳选择是将其用作后备。

if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(
     function(p) { 
     //get position from p.coords.latitude and p.coords.longitude 
     }, 
     function() { 
      //denied or error 
      //use google.loader.ClientLocation 
     } 
    ); 
} else { 
    //use google.loader.ClientLocation 
} 
+0

亚当感谢名单,我会尝试,但由于我对谷歌地图很陌生,我对如何使用Google定义的当前位置和功能缺乏了解......这是真正的梦想,但没有一个简单的方法来做到这一点,只使用google map v3,Thanx –

0
jQuery('#map_canvas').gmap({ 'zoom':2,'center':new google.maps.LatLng(39.809734,-98.55562), 'callback': function() { 

       var self = this; 
       self.getCurrentPosition(function(position, status) { 
        if (status === 'OK') { 

         clientlat = position.coords.latitude; 
         clientlon = position.coords.longitude; 
         var clientlatlng = new google.maps.LatLng(clientlat, clientlon); 

        } 
       }); 

      }});