Bring Google Map Markers to Front by Changing Z-In

2019-06-17 02:28发布

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

4条回答
该账号已被封号
2楼-- · 2019-06-17 02:40

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楼-- · 2019-06-17 02:41

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楼-- · 2019-06-17 02:53

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));
查看更多
放我归山
5楼-- · 2019-06-17 03:05

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

simply call :

marker.showInfoWindow();
查看更多
登录 后发表回答