简体   繁体   English

在Google Maps api中使用邻域类型获取formatted_address - 反向地理编码

[英]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.

相关问题 Google Maps API formatted_address - Google Maps API formatted_address Google Maps-从合并结果中获取结果[0] .formatted_address [1] .formatted_address - Google Maps - Getting results[0].formatted_address from concatenated results[1].formatted_address 从google maps api json获取formatted_address - get formatted_address from google maps api json Google Maps API:反向地理编码和地址打印 - Google Maps API: reverse geocoding and address printing 在某些地方,“ formatted_address”为null,但在某些地方不是全部(Google Maps API),为什么呢? - 'formatted_address' is null for some places, but not all ( Google Maps API ) Why is this? 使用Google Maps API进行反向地理编码 - Reverse geocoding with Google Maps API Google Maps api 3中的反向地理编码 - Reverse Geocoding in Google Maps api 3 从GeocoderResult获取formatted_address组件 - Getting formatted_address components from GeocoderResult 如何使用 react-google-maps 从两个不同的 SearchBox 中获取 formatted_address? - How to get formatted_address out of two different SearchBoxes with react-google-maps? 更改由Google Places API自动完成方法调用的formatted_address的语言 - Changing the language of the formatted_address called by google Places API autocomplete method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM