1

我尝试使用谷歌地图API自动完成,但有它和地理编码API,例如之间存在一些差异:谷歌地图API自动完成地理编码X

在自动完成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里面,有没有解决办法或谷歌自动完成是不是可靠?

这里是放置代码:

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

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

文档不是很清楚:*以下类型**可能会在地点搜索结果中返回**,除了上面表1中的类型。*请参阅https://developers.google.com/places/documentation/supported_types#table2因此,如果*可能会被返回*,我们并不真正知道它何时以及为何会/将不会被退回。 – MrUpsidown 2014-10-29 16:19:56

+0

基本上,地理编码文档也非常模糊:*除上述之外,地址组件**可能包含以下类型*。请参阅:https://developers.google.com/maps/documentation/geocoding/ #Types – MrUpsidown 2014-10-29 16:25:33

+0

我报告过地图API的错误,它应该像Geocoder API一样。我写了一个回退,如果street_number为空,获取输入文本并进行地理编码请求 – Alexandre 2014-10-29 16:54:41

回答

0

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

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

所以,你可以得到这样的无功发生地= 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); 
    } 
}); 
相关问题