繁体   English   中英

phonegap geocoder.geocode从结果中获取门牌号码

[英]phonegap geocoder.geocode get the house number from results

我有此地址解析器代码:

function codeLatLng(lat, lng) {
 geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      console.log(results)
        if (results[1]) {
         //formatted address
         alert(results[0].formatted_address)//this is a string of all location 

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

我想将街道和门牌号保存在不同的变量中,该怎么办?

根据文档 ,您可以从Geocoding API响应中获取address_components ,而不是获取formatted_address ,然后获取例如street_number

"address_components" : [
    {
       "long_name" : "1600",
       "short_name" : "1600",
       "types" : [ "street_number" ]
    },
    ...
]

您可以在address_components迭代所需的组件(在此示例中,我正在检索street_number ):

for (var i = 0; i < results[0].address_components.length; i++) {
    var address_components = results[0].address_components[i];
    if (address_components.types[0] == 'street_number') {
        console.log(address_components.long_name);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM