在Google Maps v3中将标记置于所有其他标记之前

12

有人可以帮我把当前位置放在所有其他位置的前面吗?我已经了解过MAX_ZINDEX和setZindex(),但我无法理解。这是我的代码:

var latlng = new google.maps.LatLng(lat,lng);
var image = "";
var currentplace = "Ohla Hotel";
var zInd = "";
if( name == currentplace ) {
    image = "../img/hotel_icon.png";
}
else {
    image = "../img/home_icon.png";
}
//alert();
var maxZindex = google.maps.Marker.MAX_ZINDEX;
if( name == currentplace ) {
    zInd = (maxZindex + 1);
}
var locationDescription = this.locationDescription;
var marker = new google.maps.Marker({
    map: map,
    position: latlng,
    title:'pk:'+name,
    MAX_ZINDEX: zInd,
    icon: image
});
bounds.extend(latlng);
setMarker(map, marker, name, locationDescription);
2个回答

15

MAX_ZINDEX是一个常量,而不是标记的属性。

相关的MarkerOption是zIndex

var marker = new google.maps.Marker({
    map: map,
    position: latlng,
    title:'pk:'+name,
    zIndex: zInd,
    icon: image
});

这将把标记的 zIndex 属性设置为您计算出的值,该值比 API 的最大值多1。


3
大部分代码都是有意义的,但是你关于MarkerOptionsapi-doc对象的定义不太对。尝试将你的标记创建代码更改为以下内容:
var marker = new google.maps.Marker({
    map: map,
    position: latlng,
    title:'pk:'+name,
    zIndex: zInd,
    icon: image
});

既然您已经将zInd的值设置为大于google.maps.Marker.MAX_ZINDEX,那么修改后这个标记应该会在zIndex上更高,在地图上其他标记之上。


我已经尝试过´zIndex: 999´,但它根本不起作用,会给我一个错误位置。但是,如果我这样做´google.maps.event.addListener(marker, 'mouseover', function() { marker.setZIndex(9999); ib.close(); //ib.setContent($infoWindowContent[0]); ib.open(map, marker);´,它只在悬停时起作用,我需要的是当前位置在前面。 - outman
当您尝试使用MarkerOptions.zIndex属性时,您会遇到什么错误? - Sean Mickey
该地图未定位在纬度和经度上。 - outman
你的意思是标记没有定位在纬度和经度上吗?地图上? - Sean Mickey
我很难理解你的英语,并确定问题的确切位置。我回答了你在问题中提交的代码,但是你说过已经尝试过我回答中的代码。如果您想使用当前代码和关于尝试过什么的详细信息更新您的问题,我会尽力帮助。否则,我将不得不承认我无法理解你的意思。也许你可以提供一个链接到你的页面或设置一个jsFiddle? - Sean Mickey

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