如何在谷歌地图 v3 中显示/隐藏 MarkerCluster?

发布于 2024-09-29 12:58:54 字数 605 浏览 6 评论 0原文

我需要为不同的 mapType 提供不同的标记,并且我将它们推送到 MarkerClusterer

我用以下命令“隐藏”标记

cluster.set("map", null);
cluster.resetViewport();
cluster.redraw();

并用以下命令“显示”它们:

cluster.set("map", MAP);
cluster.resetViewport();
cluster.redraw();

问题是 MarkerClusterer 似乎不喜欢 set("map", null);它抛出错误 TypeError: Object #;没有方法“删除”。我怎样才能以正确的方式显示/隐藏它们?

I need to have different markers for different mapTypes, and I'm pushing them to a MarkerClusterer.

I "hide" the markers with:

cluster.set("map", null);
cluster.resetViewport();
cluster.redraw();

And "show" them with:

cluster.set("map", MAP);
cluster.resetViewport();
cluster.redraw();

The problem is that MarkerClusterer seems to not like set("map", null); it throws the error TypeError: Object #<a MarkerClusterer> has no method 'remove'. How can I show/hide them the proper way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一枫情书 2024-10-06 12:58:54

在 Javascript API v3 中,这样说就足够了:

clusterer.setMap(null);

如果将地图设置回现有地图对象,则簇将重新出现。

clusterer.setMap( this.map );

另外,我建议不要将您的集群器命名为“集群”,就像您的示例中那样。 MarkerClusterer 包含 Cluster 对象,它们是实际的聚类标记,而不是 ClusterER 本身。

In the Javascript API v3 it is sufficient to say:

clusterer.setMap(null);

If you set your map back to the existing map object, the clusters will reappear.

clusterer.setMap( this.map );

Also, I would suggest not to name your Clusterer 'cluster', like in your example. The MarkerClusterer contains Cluster objects, which are the actual clustered markers and not the ClusterER itself.

天涯沦落人 2024-10-06 12:58:54

清除集群的优雅方式

cluster.clearMarkers();

Elegant way to clear the cluster

cluster.clearMarkers();
秉烛思 2024-10-06 12:58:54

这是一个更完整的解决方案:

在 .html 添加:

<div id="map-canvas-hidden" style="display:none"></div>
<div id="map-canvas-shown" style="width:500px; height:500px"></div>

在 .js 添加:

MarkerClusterer.prototype.remove = function() { };
var HIDDEN_MAP = new google.maps.Map(document.getElementById("map-canvas-hidden"), {});
var gmap = new google.maps.Map(document.getElementById("map-canvas-shown"), {});

显示集群:

    cluster.setMap(gmap);
    cluster.resetViewport();
    cluster.redraw();

隐藏集群:

    cluster.setMap(HIDDEN_MAP);
    cluster.resetViewport();
    cluster.redraw();

最后,我需要对markerclusterer.js进行以下修补程序:

--- markerclusterer.js.orig 2013-12-06 18:02:32.887516000 +0100
+++ markerclusterer.js  2013-12-06 18:03:25.487516924 +0100
@@ -620,6 +620,7 @@
  */
 MarkerClusterer.prototype.getExtendedBounds = function(bounds) {
   var projection = this.getProjection();
+  if (!projection) return null;

   // Turn the bounds into latlng.
   var tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
@@ -657,7 +658,7 @@
  * @private
  */
 MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) {
-  return bounds.contains(marker.getPosition());
+  return bounds ? bounds.contains(marker.getPosition()) : false;
 };

希望这会有所帮助

Here is a more complete solution:

in .html add:

<div id="map-canvas-hidden" style="display:none"></div>
<div id="map-canvas-shown" style="width:500px; height:500px"></div>

in .js add:

MarkerClusterer.prototype.remove = function() { };
var HIDDEN_MAP = new google.maps.Map(document.getElementById("map-canvas-hidden"), {});
var gmap = new google.maps.Map(document.getElementById("map-canvas-shown"), {});

to show the clusters:

    cluster.setMap(gmap);
    cluster.resetViewport();
    cluster.redraw();

to hide the clusters:

    cluster.setMap(HIDDEN_MAP);
    cluster.resetViewport();
    cluster.redraw();

finally, I needed the folowing patches to markerclusterer.js:

--- markerclusterer.js.orig 2013-12-06 18:02:32.887516000 +0100
+++ markerclusterer.js  2013-12-06 18:03:25.487516924 +0100
@@ -620,6 +620,7 @@
  */
 MarkerClusterer.prototype.getExtendedBounds = function(bounds) {
   var projection = this.getProjection();
+  if (!projection) return null;

   // Turn the bounds into latlng.
   var tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
@@ -657,7 +658,7 @@
  * @private
  */
 MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) {
-  return bounds.contains(marker.getPosition());
+  return bounds ? bounds.contains(marker.getPosition()) : false;
 };

hope this helps

秋心╮凉 2024-10-06 12:58:54

这是我的代码,用于轻松显示/隐藏地图上的集群(针对当前版本的 Maps API 和 JS-Cluster-Renderer 进行了更新)。谢谢加比。

MarkerClusterer.prototype.remove = function() {};

MarkerClusterer.prototype.hide = function() {
  this.setMap(null);
  this.resetViewport();
};

MarkerClusterer.prototype.show = function() {
  this.setMap(map); // replace map with your reference to the map object
  this.redraw();
};

// To hide the clusters:
cluster.hide();

// To show the clusters:
cluster.show();

Here is my code to easily show/hide clusters on the map (updated for the current versions of Maps API and JS-Cluster-Renderer). Thanks Gabi.

MarkerClusterer.prototype.remove = function() {};

MarkerClusterer.prototype.hide = function() {
  this.setMap(null);
  this.resetViewport();
};

MarkerClusterer.prototype.show = function() {
  this.setMap(map); // replace map with your reference to the map object
  this.redraw();
};

// To hide the clusters:
cluster.hide();

// To show the clusters:
cluster.show();
誰ツ都不明白 2024-10-06 12:58:54

我通过一点猴子补丁和一点黑客努力解决了这个问题。我仍在等待一个明确的答案,但这是我问题的解决方案,所以我也发布了这个:

MarkerClusterer.prototype.remove = function () {}

[..]

cluster.set("map", HIDDEN_MAP); // remove the clusterer
cluster.resetViewport();
cluster.redraw();

I fought my way into solving this by a little monkeypatching and a little hack. I'm still waiting for a clean answer, but this is a solution to my problem, so I'm also posting this:

MarkerClusterer.prototype.remove = function () {}

[..]

cluster.set("map", HIDDEN_MAP); // remove the clusterer
cluster.resetViewport();
cluster.redraw();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文