Search code examples
ruby-on-railsgmaps4rails

Put one marker on top of all other markers on the map


I am using gmaps4rails to show bunch of markers that represent places of interest on the map. In addition I also show one "special" marker that represent accommodation. This marker has distinct icon and I would like it to always be shown on top of all other markers regardless of the zoom.

I have tried different ways but I can't manage to put the "special" marker on top. Now all the markers are shown on the map in correct places and with correct icons but the "special" one is not on top.

Could you please advise?

This is a snippet of the relevant code. I am trying to use zIndex but probably wrong.

   markers = handler.addMarkers(<%=raw @hash.to_json %>);
   var zInd = markers.length;

    markers[zInd] = handler.addMarker(
    {
        lat: lat,
        lng: lng,
        picture: {
            url: "/images/hotel_icon.png",
            width:  50,
            height: 50
        },
        zIndex: zInd + 1
    });

Solution

  • As mentioned in the doc, options should be passed as a second argument:

    markers[zInd] = handler.addMarker(
      {
        lat: lat,
        lng: lng,
        picture: {
          url: "/images/hotel_icon.png",
          width:  50,
          height: 50
        }
      },
      {
        zIndex: zInd + 1
      }
    );