[Google Maps API 3]Marker从Clusterer中分离及Marker置于Cluster上一层的解决办法
在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上一层的解决办法的更多相关文章
- Google Maps API V3 之绘图库 信息窗口
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- Google Maps API V3 之 图层
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- Google Maps API V3 之 路线服务
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- Google maps API开发(一)(转)
一.加载Google maps API <script type="text/javascript" src="http://ditu.google.com/map ...
- Google maps API开发(二)(转)
这一篇主要实现怎么调用Google maps API中的地址解析核心类GClientGeocoder: 主要功能包括地址解析.反向解析.本地搜索.周边搜索等, 我这里主要有两个实例: 实例一.当你搜索 ...
- Google maps API开发
原文:Google maps API开发 Google maps API开发(一) 最近做一个小东西用到google map,突击了一下,收获不小,把自己学习的一些小例子记录下来吧 一.加载Googl ...
- google maps api申请的问题
现在已经改由统一的GOOGLE API控制台进行所有GOOGLE API的管理了. 方法是使用Google帐号登入 https://code.google.com/apis/console. 然后在所 ...
- Google Maps API Web Services
原文:Google Maps API Web Services 摘自:https://developers.google.com/maps/documentation/webservices/ Goo ...
- Google Maps API Key申请办法(最新)
之前的Google Maps Api的API Key很容易申请,只需要按照一个简单的表单提交部署的网站地址即可,自动生成API Key并给出引用的路径. 但是最近在处理另外一个项目的时候发现之前的这种 ...
随机推荐
- [SublimeText] Sublime Text 2 运行 Python 脚本中文路径解决方法
在 SublimeText 中直接运行 Python 脚本,出现以下报错提示: Running python -u C:\Documents and Settings\Administrator\桌面 ...
- vue回到顶部的事件
其实只需要一句代码就好了: document.documentElement.scrollTop = document.body.scrollTop = ;
- /etc/fstab文件损坏的补救措施
最近乱搞,把/etc/fstab弄坏了,导致无法进入图形界面,而且所有文件都是只读的(简直郁闷到底啊),查了好多资料什么的终于弄好了,也走了不少弯路 恩,我不喜欢扯太多东西,这个是补救的帖子,还是希望 ...
- C++播放wav音乐和音效
1. #include <mmsystem.h>#pragma comment(lib,"winmm.lib")PlaySound(TEXT("c:\\te ...
- Git学习(一)(2015年11月12日)
环境:win10 已安装git工具(如未配置环境变量需先配置环境变量) 如何配置环境变量:.我的电脑-属性-高级系统设置-环境变量-系统变量 找到path然后在变量值结尾增加路径: ;C:\Progr ...
- 一种新型聚类算法(Clustering by fast search and find of density peaksd)
最近在学习论文的时候发现了在science上发表的关于新型的基于密度的聚类算法 Kmean算法有很多不足的地方,比如k值的确定,初始结点选择,而且还不能检测费球面类别的数据分布,对于第二个问题,提出了 ...
- boost::noncopyable介绍
http://blog.csdn.net/huang_xw/article/details/8248960# boost::noncopyable比较简单, 主要用于单例的情况.通常情况下, 要写一个 ...
- router之switch
比较路由中有无switch的区别: 代码一: <Router history={history}> <Route exact path="/" component ...
- ZOJ1363 Chocolate
Chocolate Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In 2100, ACM chocolat ...
- 源码包安装Python3.6
1,安装Python3.6的依赖包 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel r ...