Google Maps: How can I change the z-index of a Marker?
Asked Answered
C

3

18

There are about 100 markers on a google map plus there is one special marker that needs to be visible. Currently, the markers around it hide it totally or partially when the map is zoomed out. I need that marker to be fully visible and I think keeping it on top of all other markers should do the trick. But I cannot find a way to modify its stacking order (z-index).

Catkin answered 6/3, 2010 at 9:51 Comment(1)
Google Maps API v3 solution is here: salman-w.blogspot.com/2012/07/…Catkin
P
5

Use the zIndexProcess option in GMarkerOptions when you create the marker that you want on top. For example:

var pt = new GLatLng(42.2659, -83.74861);
var marker = new GMarker(pt, {zIndexProcess: function() { return 9999; }});
map.addOverlay(marker);

I believe the default is to have a z-index that is the latitude of the point of the marker, so this should be fairly safe at bringing a single marker to the front. Further, this was just a simple example; you can set the z-index of all your markers in whatever simple or complex way you want. Another example is to have two functions: one for special markers and one for the rest.

var pt1 = new GLatLng(42.2659, -83.74861);
var pt2 = new GLatLng(42.3000, -83.74000);
var marker1 = new GMarker(pt1, {zIndexProcess: specialMarker});
var marker2 = new GMarker(pt2, {zIndexProcess: normalMarker});
map.addOverlay(marker1);
map.addOverlay(marker2);

function specialMarker() {
  return 9999;
}

function normalMarker() {
  return Math.floor(Math.random()*1000);
}
Puny answered 6/3, 2010 at 22:17 Comment(2)
Ah... this seems promising. But I am wondering if hard-coding 9999 is OK. Is it possible to have a marker with a z-index >= 9999?Catkin
I was hinted that I might be able to use GOverlay.getZIndex() function to find z-index of markers that are to be rendered on a given latitude, but wasn't able to get it to work :(Catkin
K
25

This is for Google Maps API 2.

For Google Maps API 3 use the setZIndex(zIndex:number) of the marker.

See: http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker

Knut answered 31/8, 2011 at 3:3 Comment(0)
P
5

Use the zIndexProcess option in GMarkerOptions when you create the marker that you want on top. For example:

var pt = new GLatLng(42.2659, -83.74861);
var marker = new GMarker(pt, {zIndexProcess: function() { return 9999; }});
map.addOverlay(marker);

I believe the default is to have a z-index that is the latitude of the point of the marker, so this should be fairly safe at bringing a single marker to the front. Further, this was just a simple example; you can set the z-index of all your markers in whatever simple or complex way you want. Another example is to have two functions: one for special markers and one for the rest.

var pt1 = new GLatLng(42.2659, -83.74861);
var pt2 = new GLatLng(42.3000, -83.74000);
var marker1 = new GMarker(pt1, {zIndexProcess: specialMarker});
var marker2 = new GMarker(pt2, {zIndexProcess: normalMarker});
map.addOverlay(marker1);
map.addOverlay(marker2);

function specialMarker() {
  return 9999;
}

function normalMarker() {
  return Math.floor(Math.random()*1000);
}
Puny answered 6/3, 2010 at 22:17 Comment(2)
Ah... this seems promising. But I am wondering if hard-coding 9999 is OK. Is it possible to have a marker with a z-index >= 9999?Catkin
I was hinted that I might be able to use GOverlay.getZIndex() function to find z-index of markers that are to be rendered on a given latitude, but wasn't able to get it to work :(Catkin
K
5

Adding on to jhanifen's answer, if you want to get your one special marker to be on top of all the rest, set it's zIndex to google.maps.Marker.MAX_ZINDEX + 1. This will make sure that it is on top of any marker on the map.

Korney answered 7/10, 2013 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.