2013-05-17 64 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); 

        } 
       }); 

      }});