當我點擊地圖以外的元素時,如何觸發「infoWindow」?在地圖之外觸發Google地圖標記
我有這樣的:
var locations = [
['Location 1', 59.917224,10.587052, 1],
['Location 2', 59.912665,10.726153, 2]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: new google.maps.LatLng(59.913869,10.752245),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
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));
}
並具有HTML:
<ul>
<li>Trigger Marker 1</li>
<li>Trigger Marker 1</li>
</ul>
什麼好的建議?謝謝!
UPDATE
製造這裏的代碼的更新:
變種位置= [ [ '位置1',59.917224,10.587052,1], [ '位置2',59.912665, 10.726153,2] ];
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: new google.maps.LatLng(59.913869,10.752245),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
var markerMap = {}
for (i = 0; i < locations.length; i++) {
markerMap[ "marker-" + i ] = marker
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
$(".marker-list").on('click', function() {
var id = $(this).find('li').attr('data-id');
alert(id)
infoWindow.open(map, markerMap[ id ]);
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
HTML:
<ul class="marker-list">
<li data-id="marker-1">Trigger Marker 1</li>
<li data-id="marker-2">Trigger Marker 1</li>
</ul>
感謝帕維爾!我明白你的主意!但無論如何我都無法工作,但我上面做了一個更新,你能看到有什麼可以改變的地方嗎? – Kim
您是否已經測試了此評論下的修補程序? – Pawel