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

标签 javascript android json google-maps google-maps-api-3

如何在 Google Maps API 中获取街区类型的 formatted_address?我正在使用 Google map 进行反向地理编码,但不幸的是,距离我所在位置最近的地址是具有 neighborhood 类型的 formatted_address。这对我来说很难解析 JSON 数组,因为它有很多内容。

这是我从 Google map 获取的示例 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));
                }
              });

它没有给我最近的地址。注意:该要点的数据因纬度/经度而异,但当我检查其他地址时,只有类型为 neighborhoodformatted_address 为我提供了距纬度和经度最近的地址郎。

最佳答案

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

关于javascript - 在 Google map api 中获取带有邻域类型的 formatted_address - 反向地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222963/

相关文章:

javascript - 从书签运行时,Bookmarklet 不起作用,但从控制台运行时可以

android - 应用程序是否有像 IOS 供应商 id 这样的唯一 id?

javascript - 如何使用 JSON 重定向到其他页面

jquery - 带 jquery、json 和上一个/下一个按钮的图库。如果 json 请求返回未定义,则画廊中断

javascript - Object.keys(KeyboardEvent) 只获取一个 "isTrusted"键而不是所有键

javascript - 如何将文本光标隐藏在 div 元素下方

javascript - 建议编写/扩展 JQuery 拖放功能,以便 DIV 也可以成为拖放区域

android - 搜索后如何重置操作栏搜索?

android - Jetpack Compose 约束布局约束不链接

javascript - react Js : Uncaught (in promise) SyntaxError: