谷歌地图在android marshmallow中不可见?

时间:2017-05-03 11:23:34

标签: android google-maps android-mapview

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

实际上我在Marshmallow获得Current lat lang(0,0),这就是为什么我在棉花糖面临问题的原因。为了得到当前的lang 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();
    }

}  

2 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

首先授予应用权限,设置 - &gt;你的应用 - &gt;许可 - &gt;已启用需要权限