1
這是我的谷歌地圖代碼,頁面有谷歌地圖與標記和表與所有位置標記是地圖下的地圖。將兩個事件處理程序合併爲一個?
var locations = [];
locations.push($key = [33.8202404, -118.3010964, "House One", "Description ...");
locations.push($key = [33.9283115, -118.2940694, "House Two", "Description ...");
locations.push($key = [33.9221547, -118.3076608, "House Three", "Description ...");
var infowindow = new google.maps.InfoWindow({});
var markers = [];
for (i = 0; i < locations.length; i++) {
markers[i] = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map,
icon: "http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.55|0|6b90ff|12.5|_|" + (i+1),
title: locations[i][2]
});
pano_marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: panorama,
icon: "http://chart.apis.google.com/chart?chst=d_map_spin&chld=2.5|0|6b90ff|55|_|" + (i+1)
});
google.maps.event.addListener(markers[i], 'click', (function(marker, i) {
return function() {
map.panTo(this.getPosition());
infowindow.setContent(locations[i][3]);
infowindow.open(map, marker);
getPanoramaInfo({location: this.getPosition(), radius: 50});
}
})(markers[i], i));
//ALMOST DUPLICATE CODE, IS THERE WAY TO NOT REPEAT IT TWICE?
document.getElementById("table_marker[" + i + "]").addEventListener('click', function() {
index = this.id.substring(7,8);
map.panTo(markers[index].position);
infowindow.setContent(locations[index][3]);
infowindow.open(map, markers[this.id.substring(7,8)]);
getPanoramaInfo({location: markers[index].position, radius: 50});
});
}
當我<div>
點擊它可以作爲點擊標記在地圖上。
<div id="table_marker[0]">House One</div>
<div id="table_marker[1]">House Two</div>
<div id="table_marker[2]">House Three</div>
基本上我有兩個事件處理一個其他的<div>
非常相似的地圖標記有沒有辦法將它們組合成一個莫名其妙的事件處理程序?