javascript – Google maps API V3 – 无法对AutocompleteService预测进行地理编码

前端之家收集整理的这篇文章主要介绍了javascript – Google maps API V3 – 无法对AutocompleteService预测进行地理编码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用google.maps.places.AutocompleteService获取地点搜索的建议,但我无法对某些预测进行地理编码.

这方面的一个例子:当我搜索“风暴河口”时,我得到的预测之一是“南非风暴河口休息营”,但是这个地址无法进行地理编码以获得格子/经度,例如:http://maps.googleapis.com/maps/api/geocode/json?address=Storms%20River%20Mouth%20Rest%20Camp,%20South%20Africa&sensor=true

有没有办法获得自动完成预测的纬度/经度值?

或者,我不明白为什么谷歌自动完成会返回我无法进行地理编码的预测.

这是我正在使用的逻辑和代码的基本示例:

var geocoder = new google.maps.Geocoder();
var service = new google.maps.places.AutocompleteService(null,{
  types: ['geocode'] 
});

service.getQueryPredictions({ input: query },function(predictions,status) {
  // Show the predictions in the UI
  showInAutoComplete(predictions);
};

// When the user selects an address from the autcomplete list
function onSelectAddress(address) {
  geocoder.geocode({ address: address },function(results,status) {
   if (status !== google.maps.GeocoderStatus.OK) {
      // This shouldn't never happen,but it does
      window.alert('Location was not found.');
    }
    // Now I can get the location of the address from the results
    // eg: results[0].geometry.location
  });
}

[编辑] – 查看一个工作示例:http://demos.badsyntax.co/places-search-bootstrap/example.html

解决方法

使用getPlacePredictions()而不是getQueryPredictions().这将返回该地点的引用,您可以使用placesService.getDetails()来检索详细信息.细节将包含场所的几何图形.

注意:placesService是一个google.maps.places.PlacesService-object.

原文链接:https://www.f2er.com/js/159210.html

猜你在找的JavaScript相关文章