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

允我心安 提交于 2020-01-03 08:18:27

问题


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


回答1:


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.




回答2:


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>



回答3:


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




回答4:


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

simply call :

marker.showInfoWindow();


来源:https://stackoverflow.com/questions/26461175/bring-google-map-markers-to-front-by-changing-z-index-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!