0
我是一個開始在JavaScript中,我知道我的代碼有點混亂。我感覺好像我可能在這裏踩了一腳,但我真的希望有一些解決方法。addeventlistener以錨點提示一個infowindow在谷歌地圖api v3
我在想什麼是將事件監聽器添加到錨點的最佳方式是。現在,我有一個循環來設置地圖上的所有標記(所以我不必每次都爲每個標記編寫代碼行),但回顧一下,我想知道是否有添加方法現在是一個事件監聽器。
我有一個頁面上的鏈接列表,我有一個數組充滿了我用來標記各種事物的數據。我需要的是能夠點擊鏈接(它說「映射它!」),併爲信息窗口提示,然後我需要切換,以便它關閉,如果另一個打開
的網站可以在這裏找到: 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);
});
}
}