在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. [Python] Python 调用 C 共享库

    Linux/Unix 平台下共享库(Shared Library)文件后缀 .so:在 Windows 平台称为动态链接库(Dynamic Link Library),文件名后缀为 .dll. 利用 ...

  2. linux下的shell操作mysql

    (1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...

  3. 支持向量机SVM进阶

    本文适合于对SVM基本概念有一点了解的童鞋. SVM基本概念: 最大边缘平面--基本原理:结构风险最小化 分类器的泛化误差 支持向量 问题描述: 请对一下数据,利用svm对其进行分类.       最 ...

  4. LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)

    题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description   在字符 ...

  5. LeetCode 13 Roman to Integer(罗马数字转为整数)

    题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description   int toNumber(char ch) { switc ...

  6. phpstorm10激活加汉化

    所用到的文件:链接:http://pan.baidu.com/s/1boNqCcJ 密码:61nz 分享文件夹中,有一个“PHP Storm激活和汉化方法.zip”文件其中有一个“resources_ ...

  7. MAC ox下配置mysql

    下载MySQL安装包:http://dev.mysql.com/downloads/mysql/ 如图,选择dmg包,下载完成,双击后打开对应的pkg包,一直点击下一步,直到安装成功 启动和停止MyS ...

  8. 如何分离p12(或pfx)文件中的证书和私钥

    p12(或者pfx)文件里一般存放有CA的根证书,用户证书和用户的私钥 假设我们有一个test.p12文件 在安装了openssl的linux服务器上执行以下命令: 提取用户证书: openssl p ...

  9. H3C系列之三层交换机文件管理

    笔者本篇文章所用h3c交换机的型号为三层交换机S3600-28TP-SI 对于文件的操作一般都在用户视图下操作,常见的有如下一些操作: 1.查看操作,常用的查看操作可以使用如下命令: <H3C& ...

  10. [转][darkbaby]任天堂传——失落的泰坦王朝(上)

    前言:   曾经一再的询问自我;是否真的完全了解任天堂这个游戏老铺的真实本质?或许从来就没有人能够了解,世间已经有太多的真相被埋没在谎言和臆测之中.作为 一个十多年游龄的老玩家,亲眼目睹了任天堂从如日 ...