繁体   English   中英

谷歌地图:在markerclusterer上方渲染标记

[英]Google Maps: Render marker above markerclusterer

我有一组标记在我的地图上。 另一组标记单独显示,我碰巧需要将它们显示在集群上方。 我尝试在集群选项对象中设置 zIndex,低于第二组标记的值,但无济于事。 知道如何解决这个问题吗?

这是可以做到的,但在你到达那里之前,这是一条非常坎坷的道路......正如 Rick 所说,问题是 MarkerClusterer 添加了一个自己的 OverlayView,它在更高的窗格上添加了它的集群图标作为常规标记。 在集群上方添加标记的唯一方法是用自己的武器击败集群者并添加自己的 OverlayView 并将标记图标标记添加到更高的窗格中( 在此处阅读有关窗格的信息)。 我不太喜欢它 - 但这是我找到的唯一方法。

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

这是一个粗略的 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');

我希望这对你有用。

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

无需创建特定的叠加层,您只需切换叠加层的父容器 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;
    }
};

希望能帮助到你。

据我所知,这是无法做到的。 簇位于比标记图像更高的窗格中。

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

您可以使用 CSS 覆盖它。

CSS

.gm-style-pbc + div > div > div:first-child{ z-index: 108 !important; }

社会保障局

.gm-style-pbc{
   + div{
     > div{
       >div{
         &:first-child{
            z-index: 108 !important;
         }
       }
     }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM