在 Google 地图 api 中获取具有邻域类型的 formatted_address - 反向地理编码

Getting formatted_address with neighborhood type in Google Maps api - Reverse geocoding

如何在 Google 地图 API 中获得 formatted_address 的邻域类型?我正在使用 Google 地图进行反向地理编码,但不幸的是,离我所在位置最近的地址是类型为 neighborhood 的 formatted_address。我很难解析 JSON 数组,因为它有很多内容。

这是我从 Google 地图 获得的示例 JSON。 https://gist.github.com/anonymous/8d5250cfc37a8cef9ab2

当我尝试使用这个时:

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

它没有给我最近的地址。注意:该要点上的数据在 latitude/longitude 上有所不同,但是当我检查其他地址时,只有类型为 neighborhoodformatted_address 给我最接近 Lat 和 Lang 的地址。

我的 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 '';
}