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

13

我有一组在地图上被聚合的标记。另一组标记是单独显示的,我需要这些标记显示在聚合标记的上方。 我尝试在聚合选项对象中设置zIndex低于第二组标记,但没有成功。 有什么想法如何解决这个问题吗?


类似的问题:https://dev59.com/eW3ds4cB2Jgan1zneRTv,https://dev59.com/DVnUa4cB1Zd3GeqPd8xy - Tomas
4个回答

5
可以做到,但这是一种比较困难的方式…如Rick所说,问题在于MarkerClusterer会在一个比普通标记更高的板块上添加自己的聚合标记。唯一的方法是使用自己的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为您完成的所有东西(例如标记图像锚处理)。该示例仅对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

4

我遇到了同样的问题,但不想处理一个新的覆盖层。

在不创建特定覆盖层的情况下,您可以通过切换覆盖层父容器的 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;
    }
};

希望这能有所帮助。

在 this.getPanes() 中的 "this" 是什么? - marcovtwout
@mouwah的回答几乎是正确的,但是地图对象没有getPanes()方法。您必须定义自己的OverlayView()类来使用getPanes()方法,并更改overlayMouseTarget窗格的zIndex - Maximus S
1
这段代码之前一直运行得很完美,但是现在谷歌改了些什么东西,它突然就停止工作了! - Tomas
我已经在这个问题上设置了赏金,所以如果您能够根据新情况调整您的答案,您就可以获得它。 - Tomas

2

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接