Google地图API V3 - 无法对自动完成服务预测进行地理编码 [英] Google maps API V3 - Can't geocode AutocompleteService predictions

查看:114
本文介绍了Google地图API V3 - 无法对自动完成服务预测进行地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 google.maps.places.AutocompleteService 获取地方搜索建议,但我无法对某些预测进行地理编码。

I'm using the google.maps.places.AutocompleteService to get suggestions for a places search, but I can't geocode some of the predictions.

一个例子是:当我搜索'风暴河口'时,我得到的一个预测是''风暴河口憩息Camp,South Africa ',但是这个地址不能被geocoded来获得经/纬度,例如: http://maps.googleapis.com/maps/api/geocode/json?address=Storms%20River%20Mouth %20Rest%20Camp,%20South%20Africa& sensor = true

An example of this: When I search for 'storms river mouth', one of the predictions I get back is 'Storms River Mouth Rest Camp, South Africa', but this address cannot be geocoded to get the lattude/longitude, eg: http://maps.googleapis.com/maps/api/geocode/json?address=Storms%20River%20Mouth%20Rest%20Camp,%20South%20Africa&sensor=true

有什么方法可以获得自动填充预测的经度/纬度值?

Is there any way to get lattitude/longitude values for the autocomplete predictions?

或者,我不明白为什么google autocomplete会返回我无法进行地理编码的预测。

Alternatively, I don't understand why google autocomplete is returning predictions that I cannot geocode.

这里是一个基本示例我是w的逻辑和代码与$:

Here is a basic example of the logic and code i'm working with:

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

[edit] - View a working example here: http://demos.badsyntax.co/places-search-bootstrap/example.html

推荐答案

使用 getPlacePredictions() in代替 getQueryPredictions()。这将为该地点返回一个参考,您可以使用 placesService.getDetails()检索详细信息。详细信息将包含该地点的几何图形。

Use getPlacePredictions() instead of getQueryPredictions() . This will return a reference for the place, which you can use to retrieve details by using placesService.getDetails() . The details will contain the geometry for the place.

注意:placesService是一个google.maps.places.PlacesService对象。

Note: placesService is a google.maps.places.PlacesService-object.

这篇关于Google地图API V3 - 无法对自动完成服务预测进行地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆