【问题标题】:phonegap geocoder.geocode get the house number from resultsphonegap geocoder.geocode 从结果中获取门牌号
【发布时间】:2016-11-07 13:16:05
【问题描述】:

我有这个地理编码器代码:

function codeLatLng(lat, lng) {
 geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      console.log(results)
        if (results[1]) {
         //formatted address
         alert(results[0].formatted_address)//this is a string of all location 

        } else {
          alert("No results found");
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }

我想将街道和门牌号保存在不同的变量中,我该怎么做?

【问题讨论】:

    标签: javascript android cordova google-maps


    【解决方案1】:

    根据the documentation,您可以从Geocoding API 响应中获取address_components,而不是获取formatted_address,然后获取例如street_number

    "address_components" : [
        {
           "long_name" : "1600",
           "short_name" : "1600",
           "types" : [ "street_number" ]
        },
        ...
    ]
    

    您可以通过address_components 迭代获得所需的组件(在此示例中我正在检索street_number):

    for (var i = 0; i < results[0].address_components.length; i++) {
        var address_components = results[0].address_components[i];
        if (address_components.types[0] == 'street_number') {
            console.log(address_components.long_name);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      相关资源
      最近更新 更多