我注意到了关于谷歌弃用
reference
而采用place_id
的信息,并想要进行实现。我正在使用AutocompleteService
,但是当我运行它时,响应中不包含place_id
,但确实包含reference
和id
。下面是我对示例页面进行的快速调整(我尝试将其放在jsfiddle上,但无法运行):<!DOCTYPE html>
<html>
<head>
<title>Retrieving Autocomplete Predictions</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<script>
// This example retrieves autocomplete predictions programmatically
// from the autocomplete service, and displays them as an HTML list.
// The predictions will include a mix of places (as defined by the
// Google Places API) and suggested search terms.
function initialize() {
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({ input: 'trafalgar square' }, callback);
}
function callback(predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
var results = document.getElementById('results');
for (var i = 0, prediction; prediction = predictions[i]; i++) {
results.innerHTML += '<li>' + prediction.description + '</li>';
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<p>Query suggestions for 'trafalgar square':</p>
<ul id="results"></ul>
</body>
</html>
有人能解释一下我缺少什么/做错了什么吗?
有人有使用AutocompleteService
和place_id
一起返回预测/建议的示例吗?
谢谢