[英]Getting formatted_address with neighborhood type in Google Maps api - Reverse geocoding
how can I get the formatted_address in Google Maps API with neighborhood type? 如何在邻域类型的Google Maps API中获取formatted_address? I'm using Google Maps for reverse geocoding but unfortunately the closest address to my location is the formatted_address with a
neighborhood
type. 我正在使用谷歌地图进行反向地理编码,但不幸的是,我所在位置最近的地址是带有
neighborhood
类型的formatted_address。 Which very hard for me to parse the JSON array since it has many content. 由于它有很多内容,因此我很难解析JSON数组。
Here's the sample JSON that I got from Google Maps . 这是我从Google地图获得的示例JSON。 https://gist.github.com/anonymous/8d5250cfc37a8cef9ab2
https://gist.github.com/anonymous/8d5250cfc37a8cef9ab2
when I try to use this: 当我尝试使用它时:
var geocoder = new google.maps.Geocoder();
var point = new google.maps.LatLng(localStorage.latitude, localStorage.longitude);
geocoder.geocode({ 'latLng': point }, function (results, status) {
if (status !== google.maps.GeocoderStatus.OK) {
alert(JSON.stringify(status));
}
// This is checking to see if the Geoeode Status is OK before proceeding
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
var address = (results[0].formatted_address);
alert(JSON.stringify(address));
}
});
It doesn't give me the closest address. 它没有给我最近的地址。 Note: The data on that gist varies on the latitude/longitude but when I check the other address, only the
formatted_address
with type neighborhood
gives me the closest address from the Lat and Lang. 注意:该要点上的数据在纬度/经度上有所不同,但是当我检查另一个地址时,只有带有
neighborhood
类型的formatted_address
给我最接近Lat和Lang的地址。
My Javascript is ropey but I guess this is how I would do it: 我的Javascript很啰嗦,但我想我会这样做:
function getAddress(results) {
if(results && results.length) {
for (var i=0; i<results.length; i++) {
if (results[i].types.indexOf('neighborhood') != -1) {
return results[i].formatted_address;
}
}
return results[0].formatted_address;
}
return '';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.