无法找到中心来放大谷歌地图markerclusterer
这可能有一个非常简单的解决方案,但我遇到了麻烦。我有一个简单的标记聚类器。它聚集了两点: https://newsinteractive.post-gazette.com/dev/test.php
我也把它当作小提琴,但即使我准确地复制了我的代码,小提琴也不起作用: https://jsfiddle.net/LNMSchneiderman/zk9y1g0j/3/
当您单击标记簇,它会放大得如此之大,以至于您看不到任何街道名称或任何地标。我希望它不要放大得那么紧。我已经让它调整缩放,但现在我无法让它以集群为中心。有人可以帮忙吗?
相关代码是:
var markerClusterer = new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m', gridSize: 50, maxZoom: 15
}); //setting maxZoom doesn't fix this problem
google.maps.event.addListener(markerClusterer , 'clusterclick', function(cluster){
if (markerClusterer.isZoomOnClick()) {
//map.setCenter(cluster.latlng);//"cannot read properties of undefined"
map.panTo(markerClusterer.getCenter()); //error: not a function
this.map.setZoom(markerClusterer.getMaxZoom()+1); //works, but doesn't center on the cluster
}
});
This may have a very simple solution, but I am having trouble with it. I have a simple markerclusterer. It clusters two points:
https://newsinteractive.post-gazette.com/dev/test.php
I also have it as a fiddle, but even though I copied my code exactly, the fiddle isn't working:
https://jsfiddle.net/LNMSchneiderman/zk9y1g0j/3/
The default behavior when you click the markerclusterer has it zooming in so tight that you can't see any street names or any landmarks. I would like it to not zoom in so tightly. I've gotten it to adjust the zoom, but now I can't get it to center on the cluster. Can anyone help?
The relevant code is:
var markerClusterer = new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m', gridSize: 50, maxZoom: 15
}); //setting maxZoom doesn't fix this problem
google.maps.event.addListener(markerClusterer , 'clusterclick', function(cluster){
if (markerClusterer.isZoomOnClick()) {
//map.setCenter(cluster.latlng);//"cannot read properties of undefined"
map.panTo(markerClusterer.getCenter()); //error: not a function
this.map.setZoom(markerClusterer.getMaxZoom()+1); //works, but doesn't center on the cluster
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的建议是确定您想要的最大缩放,然后编写一个自定义
clusterclick
侦听器,该监听器缩放到集群边界,然后如果缩放大于您想要的最小值(例如 18),请更改将缩放映射到该最小值:单击群集后生成的缩放:

概念证明小提琴
代码片段:
My suggestion would be to determine the maximum zoom you want, then write a custom
clusterclick
listener, which zooms to the cluster bounds, then if the zoom is greater than your desired minimum (say 18), change the map zoom to that minimum value:resulting zoom after clicking on the cluster:

proof of concept fiddle
code snippet:
嗯,这有点不准确,但我所做的是:
Well, this is sort of inexact, but what I did was this: