Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify the z-index of Google Map Markers

I am having a Google Map with lots of markers added using websockets. I am using custom marker images based on data availability. I want to make sure the newest marker stays on top of all other elements in the map.

How do I do this?

Code:

circleIcon = {
    path: google.maps.SymbolPath.CIRCLE,
    fillColor: "blue",
    fillOpacity: 1,
    scale: 2,
    strokeColor: "red",
    strokeWeight: 10
};

coordinates = image[2].split(',');

pin=new google.maps.LatLng(coordinates[0], coordinates[1]);

if(marker) {
    marker.setMap(null);
}

if(image[0] != "NOP") {
    var markerIcon = circleIcon;
} else {
    var markerIcon = image_car_icon;
}

marker=new google.maps.Marker({
    position:pin,
    icon:markerIcon
});

marker.setMap(map);

map.setCenter(marker.getPosition());

Update

I am using jquery to update the z-index of the InfoWindow, like:

popup = new google.maps.InfoWindow({
    content:'<image id="pin_' + pin_count + '" src="data:image/png;base64,' + image[1] +'"/>',
});

popup.open(map, marker);

google.maps.event.addListener(popup, 'domready', function() {
    console.log("DOM READY...........................");

    // Bring latest Image to top            
    e = $('#pin_' + pin_count).parent().parent().parent().parent();
    e.css({
        'z-index' : 99999 + pin_count,
    });
});

Maybe that's why the marker shows behind the InfoWindow. I tried updating using zIndex, but the marker still shows behind the InfoWindow:

marker=new google.maps.Marker({
    position:pin,
    zIndex: 9999999 + pin_count,
    icon:markerIcon
});

Update

Sample code. I expect the "green circle" icon marker to be behind the "pin" icon marker.


<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?key=&sensor=false"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script>

    var london = new google.maps.LatLng(51.508742,-0.120850);

    function initialize()
    {
        var pin1=new google.maps.LatLng(51.508742, -0.120850);
        var pin2=new google.maps.LatLng(51.508642, -0.120850);

        var mapProp = {
          center:pin1,
          zoom:17,
          disableDefaultUI:true,
          mapTypeId:google.maps.MapTypeId.ROADMAP
          };

        var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

        var marker=new google.maps.Marker({
          position:pin2,
          zIndex:99999999
          });

        marker.setMap(map);

        var infowindow = new google.maps.InfoWindow({
          content:"Hello World!"
          });

         infowindow.open(map,marker);

        circleIcon = {
            path: google.maps.SymbolPath.CIRCLE,
            fillColor: "blue",
            fillOpacity: 1,
            scale: 2,
            strokeColor: "green",
            strokeWeight: 10
        };

        var marker2=new google.maps.Marker({
          position:pin1,
          icon:circleIcon,
          zIndex:99999
          });

        marker2.setMap(map);
    }

    google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>

</body>
</html> 

like image 408
ATOzTOA Avatar asked Dec 25 '12 22:12

ATOzTOA


People also ask

What are Google map markers?

A marker identifies a location on a map. By default, a marker uses a standard image. Markers can display custom images, in which case they are usually referred to as "icons." Markers and icons are objects of type Marker .

How do I find the latitude and longitude of a marker on Google Maps?

addListener(myMarker, 'dragend', function(evt){ document. getElementById('current'). innerHTML = '<p>Marker dropped: Current Lat: ' + evt. latLng.

How do I change the color of a marker in Google Maps API?

Add different color markerspng at the end of the URL to get a blue marker. To add a green marker simply change it to green-dot.


2 Answers

You have to set the marker option "optimized" to false in combination with the zIndex option.

var marker=new google.maps.Marker({       position:pin2,       optimized: false,       zIndex:99999999 }); 

This should solve your problem.

like image 135
K. Wils Avatar answered Sep 22 '22 06:09

K. Wils


By default map will make z-index higher for markers lower on the map so they visibly stack top to bottom

You can set zIndex option for a marker. You would just need to create a increment zIndex counter as you add markers and add that to the marker options object:

marker=new google.maps.Marker({
    position:pin,
    icon:markerIcon,
    zIndex: counterValue
});

I'm not quite sure what the base start value needs to be

like image 29
charlietfl Avatar answered Sep 23 '22 06:09

charlietfl