2016-10-01 120 views
1

我正在基於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」。

+0

是你的代碼拋出一個錯誤?在你的標記代碼中,你在使用'map'的infowindow代碼中使用'map.instance'。 – Robiseb

+0

不,我的代碼沒有發生錯誤。 它正在工作......但應用程序只顯示數組'list'的第一個位置,我需要它顯示'list'中的所有位置。 –

+0

從一個[簡單](https://developers.google.com/maps/documentation/javascript/examples/marker-simple)工作示例開始,並使用動畫功能逐漸展開。 –

回答

1

可能是因爲您正在使用infowindow變量而沒有任何聲明。

marker.addListener('click', function() { 
    infowindow.setContent(contentString); 
    infowindow.open(map, marker); 
}); 

之前創建你的循環一個信息窗口對象設置標記聽者這樣的:

let infowindow = new google.maps.InfoWindow({ 
    content: contentString 
}); 

More information in the Google Maps API documentation

編輯

變化還有那些線

list.forEach(function(offer, index) { // <--- change for index 
    console.log(offer); 
    // [...] 

    // Add the infowindow for each marker 
    marker.addListener('click', function() { 
    infowindow.setContent(contentString); // <--- remove this one 
    infowindow.open(map, marker); // <--- map or map.instance ?? 
    }); 

    // Animation Timeout 
    setTimeout(function() { 
    // [...] 
    }, index * 200); // <--- change for index 
}); 

之後,您可以刪除所有count引用。

對於infowindow.open(map, marker)map引用應該與標記相同。

new google.maps.Map()是什麼變量? map.instancemap

+0

謝謝你的建議! 我創建了infowindow對象,但它並未顯示「列表」中的所有位置。 –

+0

我很確定問題是'map' /'map.instance'混淆。 – Robiseb

+0

我改變了代碼,就像你說的。 我刪除了所有的'count'引用並添加'index' 然後我用'map'和'map.instance'測試這個應用程序但是它不起作用! Answear你的問題,沒有'new google.maps.Map()',地圖是用這個操作創建的 'GoogleMaps.ready('exampleMap',function(map){ [012]] map :map.instance, }); ' –

相關問題