Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps v2 Marker zOrdering - Set to top

Tags:

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 i do this?

I have tried adding this marker before and after all other markers, however the ordering seems to be from bottom (front) to top (back) on the map view. This is not acceptable as I want to center on a marker which is in front of all markers.

I know that Polygons can be zOrdered but I would MUCH prefer a nicely styled graphic!

In-fact you can't order a Polygon over a marker!

"The order in which this polygon is drawn with respect to other overlays, including Polylines, GroundOverlays and TileOverlays, but not Markers. An overlay with a larger z-index is drawn over overlays with smaller z-indices. The order of overlays with the same z-index value is arbitrary. The default is 0." - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polygon

Does anyone have any ideas on this?

like image 251
Aiden Fry Avatar asked Feb 08 '13 11:02

Aiden Fry


People also ask

How do I get a marker position on Google Maps?

You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.

How do you move a marker in maps?

Use a long press to activate the ability to move the marker. By default, when a user taps a marker, the map toolbar appears at the bottom right of the map, giving the user quick access to the Google Maps mobile app.

How do you drag and drop a red marker on a map?

Place a tick in the small box on the Location – Marker is placed incorrectly on the map line. Do this by placing the cursor over the box and left click. You will now be able to drag the marker to the correct position. This is done by placing your cursor over the red marker, left click and hold the mouse button down.


2 Answers

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.

like image 53
Bastien Beurier Avatar answered Nov 27 '22 16:11

Bastien Beurier


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)

like image 28
passsy Avatar answered Nov 27 '22 17:11

passsy