【问题标题】:Google Map in not Visible in android marshmallow?谷歌地图在安卓棉花糖中不可见?
【发布时间】:2017-05-03 11:23:34
【问题描述】:

当构建 API 级别大于 23 的应用程序时,Google 地图未显示在棉花糖设备中,但它在棉花糖版本以下正确显示。我使用了 MapView。

实际上,我在 Marshmallow 中得到了 Current lat lang (0,0),这就是我在 marshmallow 中遇到问题的原因。为了获取当前的 lat lang,我正在使用这条线:-

private void getCurrentLatLong() {

    try {
        gps = new GPSTracker(getActivity());

        if (gps.canGetLocation()) {

            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();

            System.out.println("latitude print==" + latitude);


            lat = String.valueOf(latitude);
            lng = String.valueOf(longitude);
            System.out.println("lat long check====" + "lati :" + lat + " -- lng :" + lng);

            if (lat.equalsIgnoreCase("0.0") || lng.equalsIgnoreCase("0.0")) {
                getCurrentLatLong();
            } else {
                mMapView.onResume();
                setMap();
            }

        } else {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}  

【问题讨论】:

  • 您介意展示您的代码吗?
  • 如果发生错误,显示您的代码以及错误 logcat
  • @Luiz Fernando Salvaterra 实际上,当我构建高于 23 的应用程序时,我没有得到当前的纬度,它在棉花糖设备中总是 (0,0),但低于棉花糖它的工作正常

标签: android google-maps android-mapview


【解决方案1】:

首先这个权限必须有

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

那么,当你想使用位置图时,需要运行时权限

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

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(Maps_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(Maps_Activity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
    }
}
else{
     //load your map here
}

并且覆盖 onRequestPermissionsResult。

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                // load your map here also

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

【讨论】:

    【解决方案2】:

    先授予应用权限,设置->你的应用->权限->启用需要权限

    【讨论】:

    • 并编写代码以启用权限或在运行时加载地图之前询问权限以避免问题
    • 我已经做了所有这些事情,但仍然面临这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 2023-03-17
    • 2016-01-13
    • 2014-02-23
    相关资源
    最近更新 更多