【问题标题】:Getting formatted_address with neighborhood type in Google Maps api - Reverse geocoding在 Google Maps api 中使用邻域类型获取 formatted_address - 反向地理编码
【发布时间】:2015-03-29 03:58:39
【问题描述】:

我怎样才能在 Google Maps API 中使用街区类型获取 formatted_address?我正在使用 Google 地图进行反向地理编码,但不幸的是,离我最近的地址是 formatted_address,其类型为 neighborhood。这对我来说很难解析 JSON 数组,因为它有很多内容。

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

它没有给我最近的地址。注意:该要点上的数据因纬度/经度而异,但是当我检查其他地址时,只有 formatted_address 类型为 neighborhood 的地址是距离 Lat 和 Lang 最接近的地址。

【问题讨论】:

  • 您使用什么 Javascript 库?查询?下划线?您可能应该删除 Android 标签
  • Angularjs。它是由stackoverflow建议的。我正在使用cordova作为android应用程序运行。
  • Angular 可能提供了一些漂亮的方法来循环检查results[i].types 是否包含"neighbourhood",然后使用results[i].formatted_address。当然也可以用纯 Javascript 来完成。如果结果不包含类型为 neighbourhood 的地址,您需要回退。

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


【解决方案1】:

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

【讨论】:

  • 谢谢。我稍后会试试这个。我现在正在使用手机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
相关资源
最近更新 更多