我有一组标记聚集在我的 map 上。 另一组标记单独显示,我恰好需要将它们显示在集群上方。 我尝试在集群选项对象中设置 zIndex,低于第二组标记的 zIndex,但无济于事。 知道如何解决这个问题吗?
最佳答案
这是可以做到的,但是在您到达那里之前这是一条非常崎岖的路...正如 Rick 所说,问题是 MarkerClusterer 添加了一个自己的 OverlayView,其群集图标作为常规标记位于较高的 Pane 中。在聚类上方添加标记的唯一方法是用自己的武器击败聚类者并添加自己的 OverlayView 并将标记图标标记添加到更高的 Pane (阅读有关 Pane 的信息 here )。我不太喜欢它 - 但这是我找到的唯一方法。
为此,您必须创建一个实现 google.maps.OverlayView ( reference ) 的自定义叠加层,可以找到一个很好的示例 here (通过解释,我使用了其中的一些代码)。
这是一个粗略的 CustomOverlay 原型(prototype):
// 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);
};
此叠加层获取 map 、LatLng 对象和图标的 URL。好处是您可以将自己的 HTML 写入图层,坏处是您必须自己处理 Maps API 为您做的所有事情(例如标记图像 anchor 处理)。该示例仅适用于 anchor 位于图像中间的 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');
我希望这对你有用。
关于javascript - 谷歌地图 : Render marker above markerclusterer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10243855/