首页 文章

Google Map 自动填充API X地理编码器

提问于
浏览
1

我正在尝试使用Google Maps Autocomplete API,但它与Geocoder API之间存在一些差异,例如:

在自动完成API上,我正在尝试搜索地址“Rua Tiradentes,1020,Ibirubá”,当我在列表中选择时,地址组件“street_number”没有出现,这是该地址和其他地址的问题 .

让我们尝试使用地理编码器API:

var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': 'Rua Tiradentes, 1020, Ibirubá' }, function (results, status) {
   if (status == google.maps.GeocoderStatus.OK) {
      console.log(results);
   } else {
      alert("Geocoder failed due to: " + status);
   }
});

现在,street_number位于address_components内,是否有任何解决方法或Google Autocomplete不可靠?

这是地方代码:

var b = new google.maps.places.Autocomplete(myInput);

google.maps.event.addListener(b, 'place_changed', function () {
    console.log(b.getPlace());
})

1 回答

  • 0

    可能的解决方法如下 . 您可以尝试对该地点进行地理编码以获取所有地址组件 . 最近,Google增加了使用placeId作为请求参数对地址进行地理编码的可能性 .

    您可以在文档中找到更多详细信息:https://developers.google.com/maps/documentation/javascript/geocoding https://developers.google.com/maps/documentation/javascript/3.exp/reference#GeocoderRequest

    所以,你可以得到像var place = autocomplete.getPlace();你可以获得像place.place_id这样的地方ID,并像这样执行地理编码请求:

    geocoder.geocode( { 'placeId': place.place_id}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
        console.log(results); 
      } else { 
        alert('Geocode was not successful for the following reason: ' + status); 
      } 
    });
    

相关问题