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

4

9

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

Brower answered 20/10, 2014 at 8:16 Comment(0)
T
6

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.

Tackling answered 20/7, 2016 at 12:42 Comment(2)
what is yourZIndex?Synthesis
@King yourZIndex is an integer number which is used for setting the zIndex for the marker.Tackling
H
16

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>
Hamadryad answered 9/1, 2015 at 1:40 Comment(1)
Thank you, the extra TextView was required for me to have the z-indexing get updated. I am still nervous that this could break in future library updates, but I have yet to find a supported solution to hiding the info window...Furniture
T
6

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.

Tackling answered 20/7, 2016 at 12:42 Comment(2)
what is yourZIndex?Synthesis
@King yourZIndex is an integer number which is used for setting the zIndex for the marker.Tackling
S
1

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

Scampi answered 28/4, 2015 at 17:40 Comment(0)
B
-1

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

simply call :

marker.showInfoWindow();
Biddick answered 2/4, 2015 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.