Dean Blakely Dean Blakely - 1 year ago 70
Android Question

Issue with GoogleMap .zIndex parameter

I'm upgrading my Android app to Marshmallow 23 along with the latest GoogleMaps V2. One reason I did this is because the latest maps allows a .zIndex parameter that allows us to set the z-index of stuff we draw on the map.

But my problem is that the compiler allows it's use in some of my addMarker statements but not in others.

In the following code the compiler flags .zIndex saying can't resolve method:

private void addLegMarker(GoogleMap map, double lat, double lon,
String title, String snippet, float bearing)
//this method adds the permanent pin to the map and "moves" the arrow representing the target
//following two floats put the pin point exactly on the blue line
float u = (float) 0.2;
float v = (float) 1.0;
//following float center the arrow
float center = (float) 0.5;
//float myRotation = (float) 90.0;

//add the permanent pin to the location...
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
.anchor(u, v))

if (!(lastArrowMarker == null))

String ArrowSnippet = "Last known position of target phone";
lastArrowMarker = map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.anchor(center, center)


This is my build.gradle file:

apply plugin: ''

android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
useLibrary 'org.apache.http.legacy' //httpClient not supported in 23.
defaultConfig {
applicationId "com.deanblakely.myappname"
minSdkVersion 16
targetSdkVersion 23


buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

dependencies {
compile project(':library')
//compile project(':googleplayservices_lib')
compile files('libs/gson-2.2.4.jar')
compile ''
compile ''
//compile '' too big. we just need maps and gcm
compile ''
compile ''


However this following code accepts the .zIndex just fine:

lastArrowMarker = map.addMarker((new MarkerOptions()
.position(new LatLng(myPos.latitude, myPos.longitude))
.anchor(center, center)
.snippet("Trip End")

I noticed that where it is getting flagged, the map object is passed as an argument and where it is working fine, the code refers directly to the GoogleMap named map but I don't see how that would matter.

Why is the compiler accepting this one parameter in one place and flagging it in another?

Answer Source

The MarkerOptions class has a zIndex() method but the Marker class has a setZIndex() method.

The GoogleMap.addMarker() method returns a Marker object from a MarkerOptions object (builder) and this is causing your confusion.

In the case that it's not working, you are calling zIndex() instead of setZIndex() on a Marker object.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download