Google Maps v2 Marker zOrdering - Set to top

后端 未结 3 1523
感动是毒
感动是毒 2020-12-01 06:22

I know there are a few threads around on this topic but i havent yet found a good enough solution.

I NEED to place a marker over the top of all other markers. How do

相关标签:
3条回答
  • 2020-12-01 07:03

    This was added in Google Play Services 9.2 (June 27, 2016)

    The new MarkerOptions.zIndex() sets the stack order of a marker in relation to other markers on the map. Read more about marker z-indexes and the effect of z-index on click events. (Issue 4688)

    0 讨论(0)
  • 2020-12-01 07:16

    Aiden Fry's answer works if you do actually display an InfoWindow. If you don't, the marker won't come to the front. Here is a hack to make it come to the front anyway:

    Create a custom InfoWindowAdapter that displays a 0dp info window:

    public class MapWindowAdapter implements GoogleMap.InfoWindowAdapter {
        private Context context = null;
    
        public MapWindowAdapter(Context context) {
            this.context = context;
        }
    
        // Hack to prevent info window from displaying: use a 0dp/0dp frame
        @Override
        public View getInfoWindow(Marker marker) {
            View v = ((Activity) context).getLayoutInflater().inflate(R.layout.no_info_window, null);
            return v;
        }
    
        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    }
    

    Here is the code for the layout:

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

    When you set up your map, set the adapter and custom InfoWindowAdapter and OnMarkerClickListener (using showInfoWindow and returning true as AIden Fry advised):

    mMap.setInfoWindowAdapter(new MapWindowAdapter(this));
    
    //Call showInfoWindow to put marker on top of others.
    myMarker.showInfoWindow();
    

    This is not beautiful, but I didn't find any other way to deal with z-indexing without using info windows.

    0 讨论(0)
  • 2020-12-01 07:21

    Whilst not the perfect solution! I have figured out how to show a selected (tapped) marker over all other markers by consuming the onMarkerClick event. Returning TRUE will consume this event, so we have to do the showInfoWindow and zoom to center

        @Override
        public boolean onMarkerClick(Marker marker) {
    
           //Manually open the window
           marker.showInfoWindow();
    
           //Animate to center
           sMapFrag_v2.getMap().animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition());
    
           //Consume the method
           return true;
        }
    
    0 讨论(0)
提交回复
热议问题