24

I know there are a few threads around on this topic but i havent yet found a good enough solution.

I NEED to place a marker over the top of all other markers. How do i do this?

I have tried adding this marker before and after all other markers, however the ordering seems to be from bottom (front) to top (back) on the map view. This is not acceptable as I want to center on a marker which is in front of all markers.

I know that Polygons can be zOrdered but I would MUCH prefer a nicely styled graphic!

In-fact you can't order a Polygon over a marker!

"The order in which this polygon is drawn with respect to other overlays, including Polylines, GroundOverlays and TileOverlays, but not Markers. An overlay with a larger z-index is drawn over overlays with smaller z-indices. The order of overlays with the same z-index value is arbitrary. The default is 0." - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polygon

Does anyone have any ideas on this?

Aiden Fry
  • 1,672
  • 3
  • 21
  • 45

3 Answers3

43

Aiden Fry's answer works if you do actually display an InfoWindow. If you don't, the marker won't come to the front. Here is a hack to make it come to the front anyway:

Create a custom InfoWindowAdapter that displays a 0dp info window:

public class MapWindowAdapter implements GoogleMap.InfoWindowAdapter {
    private Context context = null;

    public MapWindowAdapter(Context context) {
        this.context = context;
    }

    // Hack to prevent info window from displaying: use a 0dp/0dp frame
    @Override
    public View getInfoWindow(Marker marker) {
        View v = ((Activity) context).getLayoutInflater().inflate(R.layout.no_info_window, null);
        return v;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}

Here is the code for the layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="0dp"
              android:layout_height="0dp">
</LinearLayout>

When you set up your map, set the adapter and custom InfoWindowAdapter and OnMarkerClickListener (using showInfoWindow and returning true as AIden Fry advised):

mMap.setInfoWindowAdapter(new MapWindowAdapter(this));

//Call showInfoWindow to put marker on top of others.
myMarker.showInfoWindow();

This is not beautiful, but I didn't find any other way to deal with z-indexing without using info windows.

Bastien Beurier
  • 910
  • 1
  • 10
  • 14
  • This is the right answer. What a shame to have to do that dirty hack for such basic task. – lukasz Jul 17 '13 at 14:57
  • And btw, setting the title is not required. – lukasz Jul 17 '13 at 15:03
  • 4
    This works. No need to implement setOnMarkerClickListener, though. Just set the InfoWindowAdapter as described above and call showInfoWindow to set the marker on top. – Tore Rudberg Feb 11 '14 at 15:02
  • 2
    This does not working for me. What can be the reason? Not working at all. – tasomaniac Mar 25 '14 at 09:46
  • Return false in order to let the animate to center execute for you. – eski Jul 09 '14 at 20:54
  • 12
    Agree with @ToreRudberg, setOnMarkerClickListener wasn't necessary for me either *BUT* I had to add a TextView to my LinearLayout in no_info_window.xml and set the text of the TextView to a single space. With no TextView, or TextView with empty string text, it seems that Maps was smart enough to know there was nothing in the InfoWindow and so didn't put the marker on top. I combined this solution with MaciejGórski's solution [http://stackoverflow.com/questions/14715414/android-google-maps-v2-permanent-marker-infowindow/16506747#16506747] (in this post) to keep one marker always on top. – Eric Barr Dec 04 '14 at 21:02
  • 2
    This solution worked for me only after adding a TextView in the layout. for some reason, it did not work when the layout was empty – Assaf S. May 06 '15 at 08:24
  • 2
    Hello, I was able to solve the problem using this InfoWindowAdapter from the post, but the layout didn't work. So I used the layout in this post: http://stackoverflow.com/questions/26461175/bring-google-map-markers-to-front-by-changing-z-index-in-android – Radoslav Yordanov Oct 13 '15 at 16:50
18

This was added in Google Play Services 9.2 (June 27, 2016)

The new MarkerOptions.zIndex() sets the stack order of a marker in relation to other markers on the map. Read more about marker z-indexes and the effect of z-index on click events. (Issue 4688)

passsy
  • 5,162
  • 4
  • 39
  • 65
  • Unfortunately Xamarin doesn't support this property yet! Not relevant to the question asked, but worth nothing... – Justin Nov 22 '16 at 19:30
  • I came here for a Xamarin solution, but that being said @Justin I don't see where the OP's question involves Xamarin specifically. I am looking forward to when Xamarin brings this into their API. – Chase Florell Jan 13 '17 at 17:26
1

Whilst not the perfect solution! I have figured out how to show a selected (tapped) marker over all other markers by consuming the onMarkerClick event. Returning TRUE will consume this event, so we have to do the showInfoWindow and zoom to center

    @Override
    public boolean onMarkerClick(Marker marker) {

       //Manually open the window
       marker.showInfoWindow();

       //Animate to center
       sMapFrag_v2.getMap().animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition());

       //Consume the method
       return true;
    }
loeschg
  • 29,961
  • 26
  • 97
  • 150
Aiden Fry
  • 1,672
  • 3
  • 21
  • 45