Google maps 谷歌地图api信息盒插件和多个标记

Google maps 谷歌地图api信息盒插件和多个标记,google-maps,google-maps-api-3,maps,Google Maps,Google Maps Api 3,Maps,使用infobox插件时,如何创建具有不同内容的多个标记 我正在创建var marker1、var marker2等,但我认为这不是一个很好的方法,我在所有信息窗口中都有相同的内容…您只需要使用某种工厂生成标记,例如: function initMarkers(map, markerData) { var newMarkers = [] // Here's where all the really verbose code goes. Loop through `markerD

使用infobox插件时,如何创建具有不同内容的多个标记


我正在创建var marker1、var marker2等,但我认为这不是一个很好的方法,我在所有信息窗口中都有相同的内容…

您只需要使用某种工厂生成标记,例如:

function initMarkers(map, markerData) {
    var newMarkers = []

    // Here's where all the really verbose code goes. Loop through `markerData` to
    // create each marker.  See the full code in the js fiddle

    return newMarkers;
}

function initialize_google_map() {
    //Here the call to initMarkers() is made with the necessary data for each marker.
    //All markers are then returned as an array into the markers variable, Usually you'd 
    //get the data from server or something, here it's just shown inline.

    var markers = initMarkers(map, [
        { latLng: new google.maps.LatLng(49.47216, -123.76307), address: "Address 1", state: "State 1" },
        { latLng: new google.maps.LatLng(49.47420, -123.75703), address: "Address 2", state: "State 2" },
        { latLng: new google.maps.LatLng(49.47530, -123.78040), address: "Address 3", state: "State 3" }
    ]);
}

请使用HTML和诸如此类的内容查看完整示例。

有关信息框切换,请单击“替换”:

google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
        newMarkers[i].infobox.open(theMap, this);
        theMap.panTo(markerData[i].latLng);
    }
})(marker, i));
作者:

google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
        for ( h = 0; h < newMarkers.length; h++ ) {
            newMarkers[h].infobox.close();
        }
        newMarkers[i].infobox.open(theMap, this);
        theMap.panTo(markerData[i].latLng);
    }
})(marker, i));
google.maps.event.addListener(标记,'click',(函数(标记,i){
返回函数(){
对于(h=0;h
@donamitest您的示例非常有用,但markerData[i]没有任何属性,我想使用markerData[i]。位置。这对我有用