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

cv8*_*v87 13 javascript google-maps-api-3 google-maps-markers markerclusterer

我有一组标记可以聚集在我的地图上.另一组标记单独显示,我碰巧需要将这些标记显示在聚类上方.我尝试在群集选项对象中设置zIndex,低于第二组标记的zIndex,但无济于事.知道如何去做吗?

rka*_*see 5

这是可以做到的,但在你到达那里之前,这是一个非常坎坷的方式......正如 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);
};
Run Code Online (Sandbox Code Playgroud)

此叠加层获取地图、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');
Run Code Online (Sandbox Code Playgroud)

我希望这对你有用。


Ric*_*ick 2

据我所知这是不可能的。簇位于比标记图像更高的窗格中。

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