GoogleMap .zIndex 参数问题

Issue with GoogleMap .zIndex parameter

我正在将我的 Android 应用升级到 Marshmallow 23 以及最新的 GoogleMaps V2。我这样做的一个原因是因为最新的地图允许一个 .zIndex 参数,它允许我们设置我们在地图上绘制的东西的 z-index。

但我的问题是编译器允许它在我的某些 addMarker 语句中使用,但不允许在其他语句中使用。

在以下代码中,编译器将 .zIndex 标记为无法解析方法:

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)
                    ));

}

这是我的 build.gradle 文件:

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'

}

然而,以下代码可以很好地接受 .zIndex:

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)

我注意到在它被标记的地方,地图对象作为参数传递,在它工作正常的地方,代码直接引用名为 map 的 GoogleMap,但我不明白这有什么关系。

为什么编译器在一个地方接受这个参数并在另一个地方标记它?

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

GoogleMap.addMarker() 方法 returns 来自 MarkerOptions 对象(构建器)的 Marker 对象,这会引起您的困惑。

在它不起作用的情况下,您在 Marker 对象上调用 zIndex() 而不是 setZIndex()

请尝试使用 equals() 而不是 ==

我还没有实际尝试过,这似乎是一个有点奇怪的解决方案,但正如 Handle marker events

中提到的

When the event occurs on one of the markers on the map, the listener's callback will be invoked with the corresponding Marker object passed through as a parameter. To compare this Marker object with your own reference to a Marker object, you must use equals() and not ==.