单击地图外的元素时如何触发“infoWindow”?
我有这个:
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>
有什么好主意吗?谢谢!
更新
在这里更新了代码:
var locations = [ ['位置 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>