我想做的是显示带有一些图钉的 Google 地图,并且在放大和缩小时我想对标记进行聚类/取消聚类。在我的 HTML 中,我已从文档加载了脚本。
<script src="https://unpkg.com/@googlemaps/markerclusterer/dist/index.min.js"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
我的代码如下所示。我初始化地图,创建标记,将它们添加到 gmap,但是当我想创建集群时,它不起作用。有谁知道我做错了什么?
var listalatlngValue = $('input[name="listalatlng"]').val();
const locations = JSON.parse(listalatlngValue);
var location = { lat: 45.9432, lng: 24.9668 };
gmap = new google.maps.Map($(this)[0], {
center: location,
zoom: 6,
});
const infoWindow = new google.maps.InfoWindow({
content: "",
disableAutoPan: true,
});
const markers = locations.map((location) => {
const marker = new google.maps.Marker({
position: { lat: location.Lat, lng: location.Lng },
map: gmap,
title: location.Text, // Set the marker's title to display the text when clicked
});
// Open an info window with the location text when the marker is clicked
marker.addListener("click", () => {
infoWindow.setContent(location.Text);
infoWindow.open(gmap, marker);
});
return marker;
});
new markerClusterer.MarkerClusterer({markers,gmap})
locations =
{Text: 'Test1', Lat: 46.3061201, Lng: 23.7056959},
{Text: 'Test2', Lat: 46.3088448, Lng: 23.761137}
发现问题了。最后一行代码应该是这样的:
新的markerClusterer.MarkerClusterer({标记:标记,地图:gmap });