如何从手机间隙的纬度和经度获取城市名称?

时间:2014-03-28 05:36:58

标签: javascript android google-maps cordova geolocation

我可以从当前纬度和经度获得完整地址。但是如何才能从完整地址获取城市名称。这是我的代码。

var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude, longitude);
//alert("Else loop" + latlng);
geocoder.geocode({
    'latLng': latlng
}, function(results, status) {
    //alert("Else loop1");
    if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
            var add = results[0].formatted_address;
            alert("Full address is: " + add);
        } else {
            alert("address not found");
        }
    } else {
        //document.getElementById("location").innerHTML="Geocoder failed due to: " + status;
        //alert("Geocoder failed due to: " + status);
    }
});

5 个答案:

答案 0 :(得分:15)

var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude, longitude);

geocoder.geocode(
    {'latLng': latlng}, 
    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var add= results[0].formatted_address ;
                    var  value=add.split(",");

                    count=value.length;
                    country=value[count-1];
                    state=value[count-2];
                    city=value[count-3];
                    alert("city name is: " + city);
                }
                else  {
                    alert("address not found");
                }
        }
         else {
            alert("Geocoder failed due to: " + status);
        }
    }
);

用","分割整个地址作为分隔符并获取城市名称..

答案 1 :(得分:5)

清理示例以从lat和lang获取位置并解析结果。基于Google Reverse Geocoding

 var geocodingAPI = "https://maps.googleapis.com/maps/api/geocode/json?latlng=23.714224,78.961452&key=YOUR_SERVER_API_KEY";

 $.getJSON(geocodingAPI, function (json) {
     if (json.status == "OK") {
         //Check result 0
         var result = json.results[0];
         //look for locality tag and administrative_area_level_1
         var city = "";
         var state = "";
         for (var i = 0, len = result.address_components.length; i < len; i++) {
             var ac = result.address_components[i];
            if (ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.short_name;
         }
         if (state != '') {
             console.log("Hello to you out there in " + city + ", " + state + "!");
         }
     }

 });

答案 2 :(得分:2)

您可以在此处找到文档:

https://developers.google.com/maps/documentation/geocoding/?hl=fr#ReverseGeocoding

但你可以在你的&#34;结果&#34;哪个项目是城市而没有拆分对象。

所以你可以这样做:

country=results[0]['address_components'][6].long_name;
state=results[0]['address_components'][5].long_name;
city=results[0]['address_components'][4].long_name;

小心,数字&#34; 4,5,6&#34;可以改变国家。所以这样测试更安全:

Getting street,city and country by reverse geocoding using google

答案 3 :(得分:1)

查看 Google Reverse Geocoding

http://maps.google.com/maps/api/geocode/xml?latlng=YourLatitude,YourLongitude&sensor=false&key=API_KEY

已经问过 here

答案 4 :(得分:1)

我正是这样做的。害怕使用逗号分隔符。

function ReverseGeoToCity(lat, lng, callback) {
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
            var properName = ", ";

            for(i=0; i<results[1].address_components.length; i++){
                if (results[1].address_components[i].types[0] == "locality")
                    properName = results[1].address_components[i].short_name + properName;
                if (results[1].address_components[i].types[0] == "administrative_area_level_1")
                    properName += results[1].address_components[i].short_name;
            }

            callback(properName);
      } else {
          alert('No results found');
      }
    } else {
        alert('Geocoder failed due to: ' + status);
    }
    });
}