cordova 如何在手机缝隙中从经纬度获取城市名称?

hlswsv35  于 2022-11-15  发布在  其他
关注(0)|答案(6)|浏览(135)

我可以从当前经纬度得到完整地址。2但是我怎么能从完整地址只得到城市名称呢?3这是我的代码。

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);
    }
});
8ljdwjyq

8ljdwjyq1#

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

使用“,”作为分隔符拆分完整地址,并获取城市名称。

uttx8gqw

uttx8gqw2#

从纬度和语言以及解析结果中获取位置的清理示例。基于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 + "!");
         }
     }

 });
k3bvogb1

k3bvogb13#

您可以在此处找到文档:
https://developers.google.com/maps/documentation/geocoding/?hl=fr#ReverseGeocoding
但是你可以在你的“结果”中看到哪个项目是城市,而不需要分割对象。
因此您可以执行:

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

注意,数字“4,5,6”可能会因国家而改变。因此,这样测试更安全:
Getting street,city and country by reverse geocoding using google

atmip9wb

atmip9wb4#

查看Google反向地理编码

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

这已经被问过了**here**

wqnecbli

wqnecbli5#

我就是这么做的。我不敢用逗号分隔符。

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

bvjveswy6#

下面的代码将帮助您从纬度和经度获得城市名称:

var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+long+"&key=KEY_HERE&sensor=false";
        $.get(url, function(data) {
        var results = data.results;
            if (data.status === 'OK') 
            {
                //console.log(JSON.stringify(results));
                if (results[0]) 
                {
                    var city = "";
                    
                   var address_components = results[0].address_components;
                    
                    for (var i = 0; i < address_components.length; i++) 
                    {  state = address_components[i].long_name;    
                        }
                        if (address_components[i].types[0] === "locality" && address_components[i].types[1] === "political" ) {                                
                            city = address_components[i].long_name;   
                        }
                    }
                    alert("CITY : " + city );
               } 
               else 
               {
                   window.alert('No results found');
               }
            } 
            else 
            {
                window.alert('Geocoder failed due to: ' + status);
            
            }
        });

相关问题