0

我是 javascript 的初学者,我知道我的代码有点乱。我觉得我可能在这里踩到了自己的脚,但我真的希望有一些解决方法。

我想知道将事件侦听器添加到锚点的最佳方法是什么。现在,我有一个循环来设置地图上的所有标记(所以我不必每次都为每个标记编写代码行)但回头看我想知道是否有办法添加现在是一个事件监听器。

我在页面上有一个链接列表,我有一个充满数据的数组,用于标记各种事物。我需要的是能够单击链接(其中显示“映射它!”)并提示信息窗口,然后我需要切换它以便在打开另一个时关闭它

该网站可以在这里找到:http: //www.michiganwinetrail.com 这里是完整的 javascript 页面 http://www.michiganwinetrail.com/mainmap2.js

我需要编辑的循环代码(可以在该 javascript 链接的底部找到)如下:

function loadMap() {


var centerMich = new google.maps.LatLng(44.229457, -85.100098);
var myOptions = {
    center: centerMich,
    zoom: 7,
    scrollwheel: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };

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

var homesouthwestDiv = document.createElement('div');
var homenorthwestDiv = document.createElement('div');
var homesoutheastDiv = document.createElement('div');
homesouthwestDiv.index = 1;
homenorthwestDiv.index = 2;
homesoutheastDiv.index = 3;
var homeControl = {
southwest: swRegions(homesouthwestDiv, map),
northwest: nwRegions(homenorthwestDiv, map),
southeast: seRegions(homesoutheastDiv, map),
};



map.controls[google.maps.ControlPosition.RIGHT_TOP].push(homesouthwestDiv);
map.controls[google.maps.ControlPosition.RIGHT_TOP].push(homenorthwestDiv);
map.controls[google.maps.ControlPosition.RIGHT_TOP].push(homesoutheastDiv);




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);
});


}


}
4

1 回答 1

-1

单击链接时,您需要执行以下操作:

var myHtml = "<h2 class=\"firstHeading\">" + winery.name + "</h2>";
map.openInfoWindowHtml(new GLatLng(winery.fltLat, winery.fltLng), myHtml);

我前段时间做过一个类似的演示:http: //dipoletech.com/beermenu/

于 2013-02-08T03:17:44.637 回答