[英]phonegap geocoder.geocode get the house number from results
I have this geocoder code: 我有此地址解析器代码:
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);
}
});
}
I want to save the street and house number in a different variables, how can I do that? 我想将街道和门牌号保存在不同的变量中,该怎么办?
According to the documentation , instead of getting the formatted_address
you can get the address_components
from the Geocoding API response, and then get for example the street_number
: 根据文档 ,您可以从Geocoding API响应中获取
address_components
,而不是获取formatted_address
,然后获取例如street_number
:
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
...
]
You can get the desired components iterating over the address_components
(I'm retrieving street_number
in this example): 您可以在
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.