0

很长一段时间以来,我都无法在我网站的酒厂 AZ 部分的链接中添加事件监听器(地图上方的绿色按钮):

http://www.michiganwinetrail.com/

问题是我之前创建了一个非常复杂的循环

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

    locations[i][0] = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        title: locations[i][3],
        map: map,
        content: locations[i][4]
    });

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

    google.maps.event.addListener(locations[i][0], 'click', function () {
        infowindow.setContent(this.content);
        infowindow.open(map, this);
    });

}

当我单击酒厂 AZ 部分中的链接时,我需要在此循环中添加什么以允许我的信息窗口弹出?如果可能的话,我想远离 jquery

4

1 回答 1

0

在位置数组中为您的位置创建一个唯一 ID,如下所示:

var locations[0] = ['twoLads',44.932317,-85.492437, '2 Lads', twoLads_content];
var locations[1] =  ['belLago',44.8529,-85.7663, 'Bel Lago', belLago_content];
//etc...

现在您可以为每个“Map It!”添加一个点击监听器。像这样的链接:

<tr class="rowClass">
<td>Bel Lago</td>
<td><a href="http://bellago.com/">Visit Website</a></td>
<td class="mapIt"><a onclick="openMarker(1);">Map It!</a></td>
</tr>

然后你可以定义一个函数来处理infoWindow的打开。

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

locations[i][0] = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    title: locations[i][3],
    map: map,
    content: locations[i][4]
});

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

google.maps.event.addListener(locations[i][0], 'click', function () {
    infowindow.setContent(this.content);
    infowindow.open(map, this);
});

}

window.openMarker = function(locationId){
    // Set content according to the locationId
    infowindow.setContent(locations[locationId][4]);
    // Open the infowindow at the location of the appropriate marker which gets stored in array key 0 in your loop above.
    infowindow.open(map, locations[locationId][0]);
}
于 2013-09-08T18:17:53.603 回答