首页 > 解决方案 > API >23 中未显示开放街道地图

问题描述

我对 osmdroid 库很陌生!我正在尝试制作一个应用程序(导航器)来检测并提醒用户有关特殊事件的信息。

我关注了 osmdroid wiki,并成功添加了低 API 的地图,但在 8.0(API 26)中,地图没有显示。我尝试添加权限,但地图一直没有显示,尽管它请求位置权限。你能建议我一个解决方案吗?在较低的 API 中,它可以完美运行。

 if (Build.VERSION.SDK_INT < 23) {
    } else {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);

        } else {

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        }

    }
}

根据 osmwiki,更高的 API 需要请求 WRITE_EXTERNAL_STORAGE、ACCESS_COARSE_LOCATION、ACCESS_FINE_LOCATION 权限。

标签: androidandroid-studioopenstreetmaposmdroid

解决方案


推荐阅读