多个“InfoWindow”用于Google地图上的“标记”数组?

时间:2015-01-21 14:40:52

标签: javascript google-maps google-maps-api-3 maps

使用Google Maps API将infoWindow添加到每个标记。标记来自阵列。

虽然,infoWindow只显示第一个标记,而不显示其他标记。为什么?感谢。

function set_markers(array) {
    var mapOptions = {
        zoom: 13
    }
    for (var i = 0; i < array.length; i++) {
        var single_location = array[i];
        var myLatLng = new google.maps.LatLng(single_location[1], single_location[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: single_location[0]
        });
        var infowindow = new google.maps.InfoWindow({
            content: ""
        });
    }

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<h3>'+this.title+'</h3>');
        infowindow.open(map,this);
    });
}

1 个答案:

答案 0 :(得分:5)

var infowindow = new google.maps.InfoWindow();

function set_markers(array) {

    var mapOptions = {
        zoom: 13
    };

    for (var i = 0; i < array.length; i++) {

        var single_location = array[i];
        var myLatLng = new google.maps.LatLng(single_location[1], single_location[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: single_location[0]
        });

        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent('<h3>' + this.title + '</h3>');
            infowindow.open(map, this);
        });
    }
}

这是未经测试的,因为您没有发布MCVE