【发布时间】:2020-01-22 16:19:07
【问题描述】:
有没有办法在 Google 地图上添加自定义图层而不会丢失点击标记的选项?
https://jsfiddle.net/hxyn9Lae/3/
到目前为止,我有这段代码,但标记不再是可点击的。而且我不想用InfoWindow,因为以后层会复杂很多。
var locations = [];
// Locations
locations = [
['1', 'Colliers Wood Station', 51.418187, -0.178096],
['2', 'Tandem Centre', 51.413675, -0.1772],
['3', 'Horatio Nelson Memorial', 51.413728, -0.192231]
];
function initialize() {
var mapOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var bounds = new google.maps.LatLngBounds();
var layer = `
<div class="map-layer">
<h6></h6>
</div>
`;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
title: locations[i][1],
position: new google.maps.LatLng(locations[i][2], locations[i][3]),
map: map
});
bounds.extend(marker.position);
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
$('#map-canvas').prepend(layer);
$('#map-canvas h6').text(locations[i][1]);
}
})(marker, i));
}
map.fitBounds(bounds);
}
initialize();
任何帮助都会很棒。
【问题讨论】:
标签: javascript google-maps google-maps-api-3 google-maps-markers