我正在使用google.maps.places.AutocompleteService
获取有关地点搜索的建议,但我无法对某些预测进行地理编码。
一个例子:当我搜索“ storms river mouth ”时,我得到的预测之一是“ Storms River Mouth Rest Camp, South Africa ”,但无法对该地址进行地理编码以获取纬度/经度,例如: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