Changing z-index (z-order) of map marker for Maps V2 in Android
I have several markers shown on my map that are either close to each other or even over top of one another. I need to have one specific marker always on top. It doesn’t matter if I add the marker to the map first or last, it often ends up being placed behind some markers. For some mysterious reason, Google Maps determines this. It needs to work with Google Maps for Android V2.
4 Solutions collect form web for “Changing z-index (z-order) of map marker for Maps V2 in Android”
The June 27th, 2016 release, v9.2.0, of Android Maps API v2 now supports a z-index – see announcement at https://developers.google.com/maps/documentation/android-api/releases#june_27_2016.
So make sure your maps/play services version is set to v9.2.0 in
The z-index documentation is below (from https://developers.google.com/maps/documentation/android-api/marker#marker_z-index):
The z-index specifies the stack order of this marker, relative to other markers on the map. A marker with a high z-index is drawn on top of markers with lower z-indexes. The default z-index value is 0.
Markers are always drawn above tile layers and other non-marker overlays (ground overlays, polylines, polygons, and other shapes) regardless of the z-index of the other overlays. Markers are effectively considered to be in a separate z-index group compared to other overlays.
You can set the index when adding the marker to the map:
map.addMarker(new MarkerOptions() .position(new LatLng(10, 10)) .title("Marker z1") .zIndex(1.0f));
… or using
Marker.setZIndex() after the marker is created.
More documentation on the effect of z-index on click events is below (from https://developers.google.com/maps/documentation/android-api/marker#marker_click_events):
- When a user clicks on a cluster of markers, the click event is triggered for the marker with the highest z-index.
- At most one event is triggered per click. In other words, the click is not passed down to the markers or other overlays with lower z-index values.
- Clicking on a cluster of markers causes subsequent clicks to cycle through the cluster, selecting each in turn. The order of the cycle first prioritises z-index, then proximity to the click point.
- If the user clicks outside the proximity of the cluster, the API recalculates the cluster and resets the state of the click cycle so that it starts from the beginning.
- The click event falls through marker clusters to other shapes and overlays before restarting the cycle.
- Markers are effectively considered to be in a separate z-index group compared to other overlays or shapes (polylines, polygons, circles, and/or ground overlays), regardless of the z-index of the other overlays. If multiple markers, overlays or shapes are overlaid on top of each other, the click event is cycled through the cluster of markers first, then triggered for other clickable overlays or shapes, based on their z-index values.
Found a solution:
This isn’t optimal because it causes the info window to appear above the marker but it’s better than nothing.
Markers in Google Map API V2 for Android do not have z-indices. They handle their own draw order
I believe this post is a duplicate of Google Maps v2 Marker zOrdering – Set to top. Bastien Beurier’s answer there uses the Marker.showInfoWindow() solution and also demonstrates how to create a 0x0 infowindow if you don’t actually want the infowindow to appear.
Also note my comment below that answer about adding a TextView to the LinearLayout.