Android 地图 - 在权限更改问题上绘制自定义标记 (Android 6.0)

Android map - drawing custom markers on permission changes issue (Android 6.0)

我在我的应用程序中使用 Google Map v2,我遇到了一个非常奇怪的问题。

在地图上,我正在绘制自定义标记 - 不同颜色的圆形对象,数字表示某种信息。

问题是,对于 Android 6.0,取消位置权限并返回应用程序后,我的地图标记使用默认图标绘制。在我重新启动应用程序之前,重新授予权限不会改变问题。

有趣的是,如果在启动应用程序时禁用位置权限,绘制我的自定义标记可以正常工作。仅当应用程序正在运行时,然后我切换位置权限(将其关闭或打开),我的标记才会更改为默认值。

有没有人知道如何解决这个问题?我想在设置中切换位置权限(这不应该与地图上的绘图标记无关)并且仍然绘制我的自定义标记而不是默认标记。

编辑: 只是为了澄清 - 我在返回应用程序时重新绘制标记。问题是它们不再是我的自定义图标,而是默认的 google 标记图标。代码:

public void addBay(final String mapMarkerId, final Position position, String status) {
    if(mapService.getMap() != null) {
        final BitmapDescriptor icon = getIconForStatus(status);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Marker newMarker = addMarkerToGoogleMap(position, icon);
                markers.put(mapMarkerId, newMarker);
            }
        });
    }
}

private Marker addMarkerToGoogleMap(Position position, BitmapDescriptor icon) {
    return mapService.getMap().addMarker(new MarkerOptions()
            .position(new LatLng(position.latitude, position.longitude))
            .anchor(0.5f, 0.5f)
            .icon(icon));
}

public BitmapDescriptor getIconForStatus(String status) {

    switch (status) {
        case BayDto.STATUS_FREE:
            return bayStatusFreeIcon;
        case BayDto.STATUS_OCCUPIED:
            return bayStatusOccupiedIcon;
        case BayDto.STATUS_UNKNOWN:
            return bayStatusUnknownIcon;
        default:
            return null;
    }
}

public void initIcons() {
    bayStatusFreeIcon = BitmapDescriptorFactory.fromBitmap(drawableUtils.createSingleMarkerFromImage(R.drawable.area_above_9_places));
    bayStatusOccupiedIcon = BitmapDescriptorFactory.fromBitmap(drawableUtils.createSingleMarkerFromImage(R.drawable.status_occupied));
    bayStatusUnknownIcon = BitmapDescriptorFactory.fromBitmap(drawableUtils.createSingleMarkerFromImage(R.drawable.status_unknown));
}

如果要添加自定义标记,将图像(标记图像)复制到可绘制文件夹中。

检查下面我的代码。

 mMap.addMarker(new MarkerOptions().position(latitude,longitude).icon(BitmapDescriptorFactory.fromResource(R.drawable.markerImage)));//markerImage is my marker image name in darwable folder.

当您返回应用程序时,在允许权限后。 在 onResume() 方法中创建标记对象或设置标记颜色。

onResume()->返回app时调用,需要在这个方法里改。

我希望我能像你期望的那样工作。

问题确实是兑现这些图标。

事情是关于权限的改变,根据这个: http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

So what will happen if permission is revoked when application is opened? I have already given it a try and found that application's process is suddenly terminated. Everything inside application just simply stopped (since it is already terminated ...). It sounds make sense to me anyway since if OS allows the application to go on its process, it may summon Freddy to my nightmare. I mean even worse nightmare than currently is ...

问题是我的应用程序没有崩溃或自行关闭。它仍然处于活动状态,我可以回去使用它。但我认为它以某种方式重新加载了自己。因此,在我通过调用 initIcons() 兑现这些图标的 activity 中,我决定像这样放置 smth:

public void onCreate(IMapActivityView mapView, boolean isFirstRun) {
    this.view = mapView;

    if(androidBuildSdkReader.getDeviceSdkVersion() >= 23)
        markerProvider.initIcons();
    else
        initIconsIfIsFirstRun(isFirstRun);
}

而不是简单地:

public void onCreate(IMapActivityView mapView, boolean isFirstRun) {
    this.view = mapView;

    initIconsIfIsFirstRun(isFirstRun);
}