Dean Blakely Dean Blakely - 3 months ago 9
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))
.title(title)
.snippet(snippet)
.anchor(u, v))
.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.cpin)
.zIndex(1.0f)
);

if (!(lastArrowMarker == null))
{
lastArrowMarker.remove();
}

String ArrowSnippet = "Last known position of target phone";
lastArrowMarker = map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.anchor(center, center)
.snippet(ArrowSnippet)
.rotation(bearing)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.smallredarrow)
.zIndex(1.0f)
));

}


This is my build.gradle file:

apply plugin: 'com.android.application'

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 'com.android.support:appcompat-v7:23+'
compile 'com.android.support:design:23+'
//compile 'com.google.android.gms:play-services:9.4.0' too big. we just need maps and gcm
compile 'com.google.android.gms:play-services-maps:9.4.0'
compile 'com.google.android.gms:play-services-gcm:9.4.0'

}


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")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.smallblackdot))
.zIndex(1.0f)


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

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.