2011-04-25 113 views
0

我正在使用JavaScript函數來獲取用戶經緯度使用谷歌地圖API。如何獲取用戶位置名稱?

$(document).ready(function() { 
     navigator.geolocation.getCurrentPosition(function (pos) { 
      var lat = pos.coords.latitude; 
      var lon = pos.coords.longitude; 
     }); 
    }); 

確實知道如何獲取位置名稱而不是經度和緯度?例如比佛利山莊,加利福尼亞州

回答

0

使用Google Maps API's Geocoding service。您可以將latlng傳遞給地理編碼請求,並在回調中預期地址的數據。

var geocoder = new google.maps.Geocoder(); 

geocoder.geocode({ 'latLng': currentLocation }, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     //the `results` variable will be an array with formatted addresses and location details 
    } 
});