开发者

How to get user location name?

开发者 https://www.devze.com 2023-02-28 10:56 出处:网络
I\'m using javascript function to get user latitude and longitude using google map api. $(document).ready(function () {

I'm using javascript function to get user latitude and longitude using google map api.

$(document).ready(function () {
        navigator.geolocation.getCurrentPosition(function (pos) {
            var lat = pos.coord开发者_StackOverflow社区s.latitude;
            var lon = pos.coords.longitude;
        });
    });

does any one know how to get location name instead of latitude and longitude? for example Beverly Hills, California


Use the Google Maps API's Geocoding service. You can pass in the latlng to the geocode request, and in the callback expect the data of the address.

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
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消