【问题标题】:Issue with GoogleMap .zIndex parameterGoogle Map .z 索引参数的问题
【发布时间】:2016-08-24 23:58:44
【问题描述】:

我正在将我的 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)

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

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

【问题讨论】:

  • 我什至在MarkerOptions 文档中都没有看到setIcon 方法。如果将其更改为 icon 会发生什么?

标签: android google-maps google-play-services z-index


【解决方案1】:

MarkerOptionshas a zIndex() methodMarkerhas a setZIndex() method

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

如果它不起作用,您将在 Marker 对象上调用 zIndex() 而不是 setZIndex()

【讨论】:

    【解决方案2】:

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

    我还没有真正尝试过,这似乎是一个有点奇怪的解决方案,但正如Handle marker events 中提到的那样,

    当事件发生在地图上的某个标记上时,将调用侦听器的回调,并将相应的Marker 对象作为参数传递。要将此 Marker 对象与您自己对 Marker 对象的引用进行比较,您必须使用 equals() 而不是 ==

    【讨论】:

    • 我的理解是 .equals 用于字符串比较。 == null 工作正常。
    猜你喜欢
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2014-12-09
    • 2012-07-05
    • 2018-02-18
    • 2012-12-11
    相关资源
    最近更新 更多