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

时间:2012-04-20 09:48:57

标签: javascript google-maps-api-3 google-maps-markers markerclusterer

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

4 个答案:

答案 0 :(得分:4)

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

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

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

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

希望它有所帮助。

答案 1 :(得分:3)

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

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

答案 2 :(得分:3)

可以这样做,但是直到你到达那里它是一个相当坎坷的方式......正如Rick所说,问题在于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');

我希望这适合你。

答案 3 :(得分:0)

您可以使用CSS覆盖它。

CSS

node* newNode(int num) {

node* temp = new node;
temp->data = num;
temp->left = temp->right = nullptr;
temp->level = 1;
return temp;

SCSS

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