[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并给出引用的路径. 但是最近在处理另外一个项目的时候发现之前的这种 ...
随机推荐
- [Linux] 如何修改 Linux 主机名
该方法适用于安装了 Linux 系统的Raspberry Pi & Cubieboard. 在终端执行: sudo vi /etc/hosts 你看到的 hosts 文件应该是这样的: 127 ...
- 操作系统定期定时执行python脚本
1. Windows 控制面板 --> 管理工具 -->任务计划程序 --> 创建任务 接下来就是设置执行的时机以及脚本路径等 1>>常规 设置任务名称描述,以及是否执行 ...
- thinkphp5.0 实现单文件上传功能
思路是:在app/ceshi/fire下面有一个index操作方法来渲染显示前端文件,然后前端文件跳转到upload操作方法进行处理,成功显示"文件上传成功",失败显示错误. 首先 ...
- Web编辑器的使用
1.复制web编辑器到你的项目中的表现层(UI) 2.添加引用:FredCK.FCKeditorV2.dll到你的项目中来 3.页面中加引用 <%@ Register TagPrefix=&qu ...
- swagger环境搭建
下面所用工具下载 http://editor.swagger.io/#/ demo 一.安装 swagger editor 说明:安装swagger前需要安装node工具 工具安装 ...
- 【咸鱼教程】BitmapLabel位图字体使用
引擎版本3.2.6 教程目录一 为什么要使用位图字体二 如何使用位图字体2.1 TextureMerger制作位图字体2.2 exml中使用位图字体三 Demo源码 一 为什么要使用位图字体egre ...
- 微信小游戏 50M那部分的缓存机制的使用
一.使用 AssetsManager 灵活定制微信小游戏的缓存策略 官网教程:http://developer.egret.com/cn/github/egret-docs/Engine2D/mini ...
- 【BZOJ5090】组题 分数规划
[BZOJ5090]组题 Description 著名出题人小Q的备忘录上共有n道可以出的题目,按照顺序依次编号为1到n,其中第i道题目的难度系数被小Q估计为a_i,难度系数越高,题目越难,负数表示这 ...
- [工具] 各种主流 SQLServer 迁移到 MySQL 工具对比
我之所以会写这篇对比文章,是因为公司新产品研发真实经历过这个痛苦过程(传统基于SQL Server开发的C/S产品转为MySQL云产品). 首次需要数据转换是测试环节,当时为了快速验证新研发云产品性能 ...
- Call removeView() on the child's parent first
extends:http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first Except ...