0
單擊每個地點圖標時,我無法獲取地點詳細信息。我閱讀了文檔,並認爲我編碼正確。任何人有任何建議?它也沒有給我所有我應該得到的結果。當真的有30個附近的酒店時,它只輸出1個酒店。謝謝。如何使用附近的Google地方搜索API查找地點詳細信息?
var map;
var infowindow;
function initMap() {
var messiah = {lat: 40.158350, lng: -76.987454};
map = new google.maps.Map(document.getElementById('map'), {
center: messiah,
zoom: 12
});
var marker = new google.maps.Marker({
position: messiah,
map: map,
title: 'Messiah College'
});
var request = {
location: messiah,
radius: 10000,
type: ['lodging']
}
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
service.getDetails(request, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
icon: {
url: 'http://maps.gstatic.com/mapfiles/circle.png',
anchor: new google.maps.Point(10, 10),
scaledSize: new google.maps.Size(10, 17)
},
position: place.geometry.location
});
var request = { reference: place.place_id };
service.getDetails(request, function(details, status) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(details.name + "<br />" + details. formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
infowindow.open(map, this);
});
});
}
謝謝!這個解決方案完美運作 –
有沒有辦法在頁面加載時顯示所有的infowindows?它看起來像我只能顯示第一個。 –
如果要打開多個infoWindows,請在createMarker()而不是initMap()中創建infoWindows,並且不要使用全局變量。 – wf9a5m75