我有一组在地图上被聚合的标记。另一组标记是单独显示的,我需要这些标记显示在聚合标记的上方。 我尝试在聚合选项对象中设置zIndex低于第二组标记,但没有成功。 有什么想法如何解决这个问题吗?
我有一组在地图上被聚合的标记。另一组标记是单独显示的,我需要这些标记显示在聚合标记的上方。 我尝试在聚合选项对象中设置zIndex低于第二组标记,但没有成功。 有什么想法如何解决这个问题吗?
// build custom overlay class which implements google.maps.OverlayView
function CustomOverlay(map, latlon, icon, title) {
this.latlon_ = latlon;
this.icon_ = icon;
this.title_ = title;
this.markerLayer = jQuery('<div />').addClass('overlay');
this.setMap(map);
};
CustomOverlay.prototype = new google.maps.OverlayView;
CustomOverlay.prototype.onAdd = function() {
var $pane = jQuery(this.getPanes().floatPane); // Pane 6, one higher than the marker clusterer
$pane.append(this.markerLayer);
};
CustomOverlay.prototype.onRemove = function(){
this.markerLayer.remove();
};
CustomOverlay.prototype.draw = function() {
var projection = this.getProjection();
var fragment = document.createDocumentFragment();
this.markerLayer.empty(); // Empty any previous rendered markers
var location = projection.fromLatLngToDivPixel(this.latlon_);
var $point = jQuery('<div class="map-point" title="'+this.title_+'" style="'
+'width:32px; height:32px; '
+'left:'+location.x+'px; top:'+location.y+'px; '
+'position:absolute; cursor:pointer; '
+'">'
+'<img src="'+this.icon_+'" style="position: absolute; top: -16px; left: -16px" />'
+'</div>');
fragment.appendChild($point.get(0));
this.markerLayer.append(fragment);
};
此叠加层获取地图、LatLng对象和图标的URL。好处是您可以编写自己的HTML代码到图层中,坏处是您必须自己处理Maps API为您完成的所有东西(例如标记图像锚处理)。该示例仅对32x32像素的图片使用良好,其中锚点位于图像中心,因此仍然相当粗糙。
要使用CustomOverlay,请按以下方式实例化:
// your map center / marker LatLng
var myLatlng = new google.maps.LatLng(24.247471, 89.920990);
// instantiate map
var map = new google.maps.Map(
document.getElementById("map-canvas"),
{zoom: 4, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP}
);
// create the clusterer, but of course with markers
//var markerClusterer = new MarkerClusterer(map, []);
// add custom overlay to map
var customCustomOverlay = new CustomOverlay(map, myLatlng, 'http://www.foo.bar/icon.png');
style="'+'width:1px; height:1px;'
,在 img 中添加 style="position: absolute; top: -THE_HEIGHT_OF_IMAGE_HEREpx; left: -HALF_OF_THE_WIDTH_OF_IMAGE_HEREpx"
,以便绘图(如经典图钉标记所示)出现在地点的正上方。 - Fanky我遇到了同样的问题,但不想处理一个新的覆盖层。
在不创建特定覆盖层的情况下,您可以通过切换覆盖层父容器的 z-indexes 来解决问题。
可以通过使用以下函数实现:
_changeOverlayOrder = function(map) {
var panes = map.getPanes();
var markerOverlayDiv = panes.overlayImage.parentNode;
var clusterOverlayDiv = panes.overlayMouseTarget.parentNode;
// Make the clusters clickable.
if(!markerOverlayDiv.style.pointerEvents) {
markerOverlayDiv.style.cssText += ";pointer-events: none;";
}
// Switch z-indexes
if(markerOverlayDiv.style.zIndex < clusterOverlayDiv.style.zIndex) {
var tmp = markerOverlayDiv.style.zIndex;
markerOverlayDiv.style.zIndex = clusterOverlayDiv.style.zIndex;
clusterOverlayDiv.style.zIndex = tmp;
}
};
getPanes()
方法。您必须定义自己的OverlayView()
类来使用getPanes()
方法,并更改overlayMouseTarget
窗格的zIndex
。 - Maximus Shttps://developers.google.com/maps/documentation/javascript/reference#MapPanes
您可以使用CSS覆盖它。
CSS
.gm-style-pbc + div > div > div:first-child{ z-index: 108 !important; }
SCSS
.gm-style-pbc{
+ div{
> div{
>div{
&:first-child{
z-index: 108 !important;
}
}
}
}
}