Home » Android » Bring Google Map Markers to Front by Changing Z-Index in Android

Bring Google Map Markers to Front by Changing Z-Index in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am creating a map based application where i show pings based on a latitude/longitude service

mMap.addMarker(new MarkerOptions()
    .position(mLatLng)
    .title("" + name)
    .snippet("" + pingDate)
    .icon(BitmapDescriptorFactory.fromBitmap(icon)));

According to google map v2 api, markers are drawn in a top to down approach, so my pings gets hidden if there are fully/partially above each other.

My question is, can i change the Z-Order axis or anything by which i can change the z position of a marker ?

Such that if i have two types of pings, i want to show one type of ping always on top irrespective of its coordinates

How to&Answers:

In latest release google map now have control over zIndex in Android.
https://developers.google.com/maps/documentation/android-api/releases#june_27_2016

You can use this now:

mMap.addMarker(new MarkerOptions()
   .position(mLatLng)
   .title("" + name)
   .snippet("" + pingDate)
   .icon(BitmapDescriptorFactory.fromBitmap(icon))
   .zIndex(yourZIndex));

From the documentation,

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.

Answer:

Tried @Simo’s answer, but it doesn’t seem to work on my phone (maybe changed with Lollipop?) – when showInfoWindow() sees that there is nothing to display it seems to skip bringing the pin to the front. I was able to fix this by using the following layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="0dp"
              android:layout_height="0dp" >

    <!-- Need a " " so that showing this isn't a no-op -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "/>
</FrameLayout>

Answer:

I believe that this post is a duplicate of Google Maps v2 Marker zOrdering – Set to top . Bastien Beurier’s answer there uses the Marker.showInfoWindow() solution to move the marker to the top. It also has more detail on how to create the 0x0 infowindow if you don’t actually want the infowindow to appear (though as I noted in a comment to that answer, the layout also needs a TextView with a space as @jt-gilkeson has shown in this post).

If you want the marker to always stay on top, then you can combine that solution with the one for Android Google Maps v2 permanent Marker InfoWindow

Answer:

if you want just want to bring it on top of all other markers

simply call :

marker.showInfoWindow();