我正在基於NodeJS的Meteor項目中工作,我需要通過Google Maps API在地圖上創建「標記」(Places)。 這些標記都處於「列表」陣列如何通過數組獲取多個值並將此地點顯示在Google地圖中?
var list = Offers.find({});
list = [
{
place: "Teste2",
offer: "Teste Offer2",
loc: {
coordinates: [-50.1681836, -25.0869446]
}
},
{
place: "Teste1",
offer: "Teste Offer1",
loc: {
coordinates: [-40.1681836, -20.0869446]
}
}];
而且這些相應的位置必須在地圖上顯示,憑藉其各自的標記的每個位置...
var count = 0;
list.forEach(function(offer) {
console.log(offer);
// Animation Timeout
setTimeout(function() {
// Create the string into the infowindow
var contentString = '<p><strong>' + offer.place + '</strong></p>' +
'<p>' + offer.offer + '</p>';
// Create the infowindow object
var infowindow = new google.maps.InfoWindow({
content: contentString
});
// Add the marker on the map
var marker = new google.maps.Marker({
position: {lat: offer.loc.coordinates[1], lng: offer.loc.coordinates[0]},
map: map.instance,
animation: google.maps.Animation.DROP
});
// Add the infowindow for each marker
marker.addListener('click', function() {
infowindow.setContent(contentString);
infowindow.open(map, marker);
});
}, count * 200);
count++;
});
然而,僅僅示出了僅第一個數組位置(「Teste2」),而不是所有的多個值由「list」。
是你的代碼拋出一個錯誤?在你的標記代碼中,你在使用'map'的infowindow代碼中使用'map.instance'。 – Robiseb
不,我的代碼沒有發生錯誤。 它正在工作......但應用程序只顯示數組'list'的第一個位置,我需要它顯示'list'中的所有位置。 –
從一個[簡單](https://developers.google.com/maps/documentation/javascript/examples/marker-simple)工作示例開始,並使用動畫功能逐漸展開。 –