Put marker from Google Maps v3 in front of all others

☆樱花仙子☆ 提交于 2019-12-05 18:02:09

问题


Can somebody help me put the current location in front of all others? I have read about MAX_ZINDEX and setZindex() but I cannot understand it. This is my code:

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

回答1:


MAX_ZINDEXis a constant, not an attribute of a marker.

The relevant MarkerOption is zIndex:

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

This sets the marker's zIndex attribute to the value you have calculated, which is one more than the API's maximum value.




回答2:


Most of your code makes sense, but your definition of the MarkerOptionsapi-doc object doesn't make sense. Try changing your marker creation code to this:

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

Since you have already set the value of zInd higher than google.maps.Marker.MAX_ZINDEX, making this change should place the marker higher in the zIndex and thus above the other markers on the map.



来源:https://stackoverflow.com/questions/11068332/put-marker-from-google-maps-v3-in-front-of-all-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!