在Google Maps API的使用中,经常用到Clusterer来避免过密的Marker显示。但仔细看一下Clusterer的设置参数中并没有直接将某些Marker除外的方法,那遇到这样的需求,怎么做呢?以下是我从StackoverFlow上获得的解答,也是实践下来最佳的方法:

// Create marker clusterer
// map: google.maps.Map Object
// markerArray: [google.maps.Marker Object1, google.maps.Marker Object2 ...]
var clusterer = new MarkerClusterer(map, markerArray, {
maxZoom: 15,
gridSize: 40
});
// Separate the map pin from the cluster
// specialMarker: google.maps.Marker Object
clusterer.removeMarker(specialMarker);
specialMarker.setMap(map);

思路很简单,就是用Clusterer对象提供的方法将Marker从中移除,然后用Marker对象的setMap方法使其回到map上。

然后你就会发现另外有一个问题出现了。这个被分离出来的Marker总是在Clusterer的下面一层。客户很不爽。看到有人说要靠建立自定义的OverlayView来把Marker画在Clusterer的上面一层,不过也没看到有具体的解决办法,于是我硬着头皮上了。对谷歌文档半熟的人来说,看到他们列出来的一堆东西很头疼,往往不知道怎么下手。

首先要知道Google Map上总共分了7个层,具体可以参考API文档OverlayView及Panes的部分,说明还是很详细的。

我们可以知道直接用Marker对象画出来的Map pin是在OverlayLayer这一层内,好像是第3层,而Cluster位于其上的OverlayImage这一层内。各层HTML按序从上到下排列,CSS是用position:absolute定位,zIndex定先后层级。

那么从这可以看出,其实如果只要显示Marker在Clusterer上方通过改变zIndex是很容易做到的。问题是,这么做因为原来的层级被搞乱导致一些事件,比如Clusterer的点击事件不能触发。

然后我用了个假的Marker画在比较高层的Pane上以假乱真。如下:

var maxZoom = 15;
var markerClusterer = new MarkerClusterer(map, markerArray, {
maxZoom: maxZoom,
gridSize: 40
});
// separate the map pin from the cluster
if (specialMarker) {
markerClusterer.removeMarker(this.centerMarker);
//specialMarker.setMap(this.map);
// Make the map pins over the clusters
if ($(map.getDiv()).find('.special-marker').length < 1) {
// Create the OverlayView structor
var overlay = function (marker) {
this.pos = marker ? marker.getPosition() : new google.maps.LatLng();
this.marker = marker;
};
var markerOverlay;
// Extend google.maps.OverlayView Class
// You must add onAdd, onRemove, Draw function to its prototype
overlay.prototype = new google.maps.OverlayView();
overlay.prototype.onAdd = function () {
var pane = this.getPanes().floatPane;
var marker = this.marker;
// Add fake marker and attach click event to it
$('<div class="special-marker"></div>').appendTo($(pane)).on('click', function () {
google.maps.event.trigger(marker, 'click');
});
};
overlay.prototype.onRemove = function () {
// You can leave this function empty
// It's only triggered when you call markerOverlay.setMap(null)
};
overlay.prototype.draw = function () {
// Calculate the position of the marker and put it on the map
// Hide the fake one when the true marker shows
var projection = this.getProjection();
var position = projection.fromLatLngToDivPixel(this.pos);
var $pane = $(this.getPanes().floatPane);
$('.special-marker', $pane).css({
left: position.x - 11,
top: position.y - 41,
position: "absolute"
}).toggle(map.getZoom() <= maxZoom);
};
markerOverlay = new overlay(specialMarker);
markerOverlay.setMap(map);
}
}

这里我先创建了一个自定义层,插入到第7层Pane的floatPane内最后。然后我用了个跟真Marker一模一样的图标作为这个层的背景,尺寸22px*41px,根据真Marker的位置计算出来在Map上的位置,用CSS绝对定位到指定位置。并且,很重要的是,把真Marker上的click事件绑定到这个假Marker上,因为点击Marker会显示InfoWindow。最后,为了避免真假Marker同时显示在Map上,加了一层判断。

实际上,以假乱真可以说是多余的,你可以直接画自己的Marker,作为真Marker来用,当然这也有一定的风险。

[Google Maps API 3]Marker从Clusterer中分离及Marker置于Cluster上一层的解决办法的更多相关文章

  1. Google Maps API V3 之绘图库 信息窗口

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  2. Google Maps API V3 之 图层

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  3. Google Maps API V3 之 路线服务

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  4. Google maps API开发(一)(转)

    一.加载Google maps API <script type="text/javascript" src="http://ditu.google.com/map ...

  5. Google maps API开发(二)(转)

    这一篇主要实现怎么调用Google maps API中的地址解析核心类GClientGeocoder: 主要功能包括地址解析.反向解析.本地搜索.周边搜索等, 我这里主要有两个实例: 实例一.当你搜索 ...

  6. Google maps API开发

    原文:Google maps API开发 Google maps API开发(一) 最近做一个小东西用到google map,突击了一下,收获不小,把自己学习的一些小例子记录下来吧 一.加载Googl ...

  7. google maps api申请的问题

    现在已经改由统一的GOOGLE API控制台进行所有GOOGLE API的管理了. 方法是使用Google帐号登入 https://code.google.com/apis/console. 然后在所 ...

  8. Google Maps API Web Services

    原文:Google Maps API Web Services 摘自:https://developers.google.com/maps/documentation/webservices/ Goo ...

  9. Google Maps API Key申请办法(最新)

    之前的Google Maps Api的API Key很容易申请,只需要按照一个简单的表单提交部署的网站地址即可,自动生成API Key并给出引用的路径. 但是最近在处理另外一个项目的时候发现之前的这种 ...

随机推荐

  1. oracle 排序字段自增长

    <insert id="insertGoodsDescription" parameterClass="goodsDescription" > &l ...

  2. 当进行make命令学习是出现error trying to exec 'cc1': execvp: No such file or directory

    进行编译的时候总是会出现这种状况 error trying to exec 'cc1': execvp: No such file or directory 自己把程序改了又改,改的很简单之后还是出现 ...

  3. linux系统抓包命令

    IP地址抓包:tcpdump -i any host 1.1.1.2 -n 端口抓包: tcpdump -i any port 6789 -n wireshak工具抓包:tcp.port == 678 ...

  4. 《转载》脚本实现从客户端服务端HTTP请求快速分析

    本文转载自https://www.imooc.com/article/14107 首先我想介绍下,分享这个脚本的用处: 当客户告知我们,一个页面http://www.xxx.com 有问题时,作为PE ...

  5. xmapp 404设置

    这样做的好处一个是很友好,另一个是对于你的网站会更安全些,如果没设置,别人在你的网址后随便输入一个路径,会显示404错误,并且会显示你的服务器版本号,服务器配置一目了然,为了避免这种情况,可以设置错误 ...

  6. 【大数据系列】windows环境下搭建hadoop开发环境使用api进行基本操作

    前言 搭建完hadoop集群之后在windows环境下搭建java项目进行测试 操作hdfs中的文件 版本一 package com.slp.hadoop274.hdfs; import java.i ...

  7. 【CSS系列】网页头部进度条方式一

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. [Vue warn]: Error in render: "SyntaxError: Unexpected token ' in JSON at position 1"

    一,场景: 字符串转对象: var str = "{'bankRate':5,'YINGUO':0}" 二,操作: JSON.parse(str)时候,报错 [Vue warn]: ...

  9. slideout

    这里在原有slideout.js增加了shade的遮罩功能 核心: 1,此插件的使用需要配合dom来用: <!-- 左边 --> <nav id="menu" c ...

  10. Apache 的mod_auth_cas模块的介绍和使用

    apache的mod_auth_cas模块是一个集成到apache中的cas客户端,一般是配合Apache的反向代理来使用,对某个url的请求先经过apache,apache会判断是否经过cas认证, ...