2011-04-25 109 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 
    } 
});