-1
我試圖在點擊地圖外部的元素時打開Google地圖標記的信息窗口。我見過幾個類似的問題,但還沒有看到有效的答案。 這裏是我的代碼:來自Google地圖外部的控制標記infowindow API
HTML:
<div id="map_canvas"></div>
<div id="names_div"></div>
腳本:
var locations = [
['Bondi Beach', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
];
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var names_html = "";
for (i = 0; i < locations.length; i++) {
names_html += " " + "<a href='javascript:myclick(" + locations.length + ")' class='Names'>" + locations[i][0] + " </a> <br> <br>" ;
};
document.getElementById("names_div").innerHTML = names_html;
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
function myclick(i) {
google.maps.event.trigger(locations[i], "click");
}
其 「names_html + = ...」 線與「功能MyClick認證(I)之間的相互作用{...「這導致我認爲的問題。
此外,我只能得到names_div的html人口與標記循環的單獨循環。這也可能是問題!
謝謝,非常感謝。