将信息窗口添加到多个标记

时间:2014-11-15 08:34:05

标签: javascript google-maps

我想将信息窗口添加到标记,分别添加到每个标记。我有以下编码。如何添加循环信息窗口。

function initialize() {
var latlng = new google.maps.LatLng(7.344102,80.517929);

var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


var service_outlets = [
    ['Muttur', 9.763324,80.200539, 5],
    ['Jaffna', 9.661498,80.025547, 6],
    ['Mannar', 8.873401, 79.989824, 7],
    ['Padaviya', 8.834935,80.759248, 8],
    ['Trincomalee', 8.566667,81.233333, 9],
    ['Matale', 7.467465,80.623416, 2]
    ];

for (var i = 0; i < service_outlets.length; i++) {
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(service_outlets[i][1], service_outlets[i][2]),
        map: map2,
      //  icon: image,
        icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',
        title: service_outlets[i][0],
        zIndex: service_outlets[i][3]
    });

    var infowindow = new google.maps.InfoWindow({
      content:service_outlets[i][0]
      });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
      });

}

}

window.onload = initialize;

单击任何标记时仅显示最后一个标记。我希望得到一些帮助

1 个答案:

答案 0 :(得分:0)

您需要在标记点击事件中设置infowindow内容,如此

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