Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>'this.getZIndex()' always returns 'undefined' if you haven't previously set a zIndex on the marker, either when initialising it in the first place or by using the setOption() function.</p> <p>Your script may also not work 100% if there are more than 100 markers.</p> <p>I've put together a very simple map below that contains 2 markers, slightly overlapping. On hover of one of the markers it will set the zIndex to the highest needed to bring it to the top, then return it back to what it was previously on mouseout:</p> <pre><code> var map; var markers = new Array(); var highestZIndex = 0; function initialize() { /* SETUP MAP */ var myLatlng = new google.maps.LatLng(52.06768, -1.33758); var mapOptions = { center: myLatlng, zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); /* ADD 1st MARKER */ var markerOptions = { position: new google.maps.LatLng(52.06768, -1.32058), map: map, zIndex:1 }; marker = createMarker(markerOptions, false); marker.set("myZIndex", marker.getZIndex()); google.maps.event.addListener(marker, "mouseover", function() { getHighestZIndex(); this.setOptions({zIndex:highestZIndex+1}); }); google.maps.event.addListener(marker, "mouseout", function() { this.setOptions({zIndex:this.get("myZIndex")}); }); /* ADD 2nd MARKER */ var markerOptions = { position: new google.maps.LatLng(52.06768, -1.33758), map: map, zIndex:2 }; marker = createMarker(markerOptions, false); marker.set("myZIndex", marker.getZIndex()); google.maps.event.addListener(marker, "mouseover", function() { getHighestZIndex(); this.setOptions({zIndex:highestZIndex+1}); }); google.maps.event.addListener(marker, "mouseout", function() { this.setOptions({zIndex:this.get("myZIndex")}); }); } function createMarker(markerOptions) { var marker = new google.maps.Marker(markerOptions); markers.push(marker); return marker; } function getHighestZIndex() { // if we haven't previously got the highest zIndex // save it as no need to do it multiple times if (highestZIndex==0) { if (markers.length&gt;0) { for (var i=0; i&lt;markers.length; i++) { tempZIndex = markers[i].getZIndex(); if (tempZIndex&gt;highestZIndex) { highestZIndex = tempZIndex; } } } } return highestZIndex; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload