首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google映射API V3 -不能对AutocompleteService进行地理编码预测

Google映射API V3 -不能对AutocompleteService进行地理编码预测
EN

Stack Overflow用户
提问于 2013-01-19 12:38:57
回答 6查看 15.1K关注 0票数 8

我正在使用google.maps.places.AutocompleteService来获得搜索地点的建议,但我无法对其中的一些预测进行地理编码。

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

有什么方法可以获得自动完成预测的延迟/经度值吗?

或者,我不明白为什么谷歌自动完成返回的预测,我不能地理编码。

下面是我正在使用的逻辑和代码的一个基本示例:

代码语言:javascript
运行
复制
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

EN

回答 6

Stack Overflow用户

发布于 2013-01-19 13:02:55

使用getPlacePredictions()而不是getQueryPredictions()。这将返回该位置的reference,您可以使用该位置通过placesService.getDetails()检索详细信息。细节将包含该地方的几何图形。

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

票数 11
EN

Stack Overflow用户

发布于 2016-07-19 06:49:30

AutocompleteService返回的预测具有PlaceId属性。根据文档PlaceId,您可以将https://developers.google.com/maps/documentation/javascript/geocoding而不是地址传递给地理编码器。

代码语言:javascript
运行
复制
var service = new google.maps.places.AutocompleteService();
var request = { input: 'storms river mouth' };
service.getPlacePredictions(request, function (predictions, status) {
    if(status=='OK'){
        geocoder.geocode({ 
            'placeId': predictions[0].place_id
        }, 
        function(responses, status) {
            if (status == 'OK') {
                var lat = responses[0].geometry.location.lat();
                var lng = responses[0].geometry.location.lng();
                console.log(lat, lng);
            }
        });
    }
});
票数 8
EN

Stack Overflow用户

发布于 2013-02-07 09:22:18

试试这个:

代码语言:javascript
运行
复制
function onSelectAddress(address, callback) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            callback(results[0].geometry.location);
        } else {
            alert("Can't find address: " + status);
            callback(null);
        }
    });
}

然后,呼叫和回调:

代码语言:javascript
运行
复制
onSelectAddress('your address here', function(location){
//Do something with location
if (location)
     alert(location);
});

对不起我的英语。我有一个问题要问你:你能给我看看showInAutoComplete() ??的方法吗?我在href列表上显示了预测,但我不知道如何保存“单击的”地址值。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14414445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档