我正在使用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
发布于 2013-01-19 13:02:55
使用getPlacePredictions()
而不是getQueryPredictions()
。这将返回该位置的reference
,您可以使用该位置通过placesService.getDetails()
检索详细信息。细节将包含该地方的几何图形。
注意: placesService是一个google.maps.places.PlacesService-对象。
发布于 2016-07-19 06:49:30
AutocompleteService返回的预测具有PlaceId属性。根据文档PlaceId,您可以将https://developers.google.com/maps/documentation/javascript/geocoding而不是地址传递给地理编码器。
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);
}
});
}
});
发布于 2013-02-07 09:22:18
试试这个:
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);
}
});
}
然后,呼叫和回调:
onSelectAddress('your address here', function(location){
//Do something with location
if (location)
alert(location);
});
对不起我的英语。我有一个问题要问你:你能给我看看showInAutoComplete() ??的方法吗?我在href列表上显示了预测,但我不知道如何保存“单击的”地址值。
https://stackoverflow.com/questions/14414445
复制相似问题