Google地图:markerclusterer上方的渲染标记

问题描述 投票:13回答:4

我有一组标记聚集在我的地图上。另一组标记分别显示,我碰巧需要将它们显示在群集上方。我试过在clusters options对象中设置zIndex,低于第二组标记的zIndex,但无济于事。任何想法如何去做?

javascript google-maps-api-3 google-maps-markers markerclusterer
4个回答
3
投票

据我所知,这无法完成。群集位于标记图像上方的窗格中。

https://developers.google.com/maps/documentation/javascript/reference#MapPanes


4
投票

我遇到了同样的问题,但不想处理新的叠加层。

无需创建特定的叠加层,您只需切换叠加层的父容器的z-indexs。

这可以通过使用以下功能来实现:

_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;
    }
};

希望有帮助。


3
投票

可以完成,但是直到到达那里都是一种坎bump的方式……正如里克所说,问题在于MarkerClusterer在更高的窗格中添加了自己的OverlayView及其簇图标作为常规标记。在群集上方添加标记的唯一方法是用自己的武器击败群集器,并添加自己的OverlayView并将标记图标标记添加到更高的窗格(请参阅窗格here)。我不太喜欢-但这是我发现的唯一方法。

为此,您必须创建一个实现google.maps.OverlayView(reference)的自定义叠加层,可以找到一个很好的示例here(有解释,我从中使用了一些代码)。

这是粗糙的CustomOverlay原型:

// 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为您所做的所有工作(例如标记图像锚点处理)。该示例仅适用于锚点位于图像中间的32x32px图像,因此仍然很粗糙。

要使用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');

我希望这对您有用。


0
投票

您可以使用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;
         }
       }
     }
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.