Google Maps JavaScript API v3动态信息窗口

时间:2012-05-17 09:34:13

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

仍然掌握谷歌地图JavaScript API v3,所以请光临我。我正在动态添加标记:

var oldMarkers = null;
    return function(map, locations){
        //Clearing markers, if they exist
        if(oldMarkers && oldMarkers.length !== 0){
            for(var i = 0; i < oldMarkers.length; ++i){
                oldMarkers[i].setMap(null);
            }
        }
        //Adding new markers
        oldMarkers = [];
        for (var i = 0; i < locations.length; i++) {
            var newmarker = locations[i];
            var myLatLng = new google.maps.LatLng(newmarker[1], newmarker[2]);
            var marker = new google.maps.Marker({
               position: myLatLng,
               map: map,
               shape: shape,
               title: newmarker[0],
               zIndex: newmarker[3],
               clickable: true
            });
oldMarkers.push( marker );
 }

基于jQuery“click”功能运行良好。我现在想做的是为每个标记添加一个信息窗口。好的,我有来自Google的代码,应该/确实在oldMarkers.push( marker )之前执行:

var j = i + 1;
marker.setTitle(j.toString());
attachSecretMessage(marker, j);

和这个功能:

function attachSecretMessage(marker, number) {
  var message = ["This","is","the","secret","message"];
  var infowindow = new google.maps.InfoWindow(
      { content: message[number],
        size: new google.maps.Size(50,50)
      });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });
}

它有点工作,因为它应该显示2个标记的信息框,但不是全部。这就是我不明白的地方:如果我有一个数组来制作这样的新标记(只是一个例子):

var essentials = [
['Marker 1', 12.254181, 12.649962 , 9],
['Marker 2', 12.254181, 12.649962 , 9],
['Marker 3', 12.254181, 12.649962 , 9],
['Marker 4', 12.254181, 12.649962 , 9],
}

我需要/想要做的是为infowindow的内容添加一个数组键,所以有点像这样:

 var essentials = [
    ['Marker 1', 12.254181, 12.649962 , 9, 'Info Window content']

如何将newmarker[4]传递给infowindow?

提前致谢

1 个答案:

答案 0 :(得分:1)

OMG我在想! - 只需添加attachSecretMessage ( marker, newmarker[4] ); ooops!说的很厚!遗憾