这是我在stackoverflow中的第一篇文章,所以如果我说错了请不要讨厌我!
我正在尝试使用本教程https://developers.google.com/maps/documentation/android-api/current-place-tutorial来处理我的片段和PermisionDispatcher。但是,我收到checkPermision错误,我不明白为什么。
我已经检查了这个答案trying to add marshmallow permissions using "Permissions Dispatcher"但是,我仍然无法做到这一点...... 这是我的片段代码:
import android.Manifest;
import android.app.FragmentManager;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;
import static android.content.ContentValues.TAG;
/**
* Created by Daumantas on 2017-05-25.
*/
@RuntimePermissions
public class AddPlacesFragment extends Fragment implements OnMapReadyCallback, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
Context mContext;
private GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;
private boolean mLocationPermissionGranted = false;
private Location mLastKnownLocation = null;
private CameraPosition mCameraPosition = null;
private LatLng mDefaultLocation = new LatLng(40.76793169992044,
-73.98180484771729);
private float DEFAULT_ZOOM = 10;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(),
this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
//.addApi(Places.GEO_DATA_API)
//.addApi(Places.PLACE_DETECTION_API)
.build();
mGoogleApiClient.connect();
return inflater.inflate(R.layout.add_places_fragment, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mContext = getContext();
FragmentManager fragment = getActivity().getFragmentManager();
MapFragment mf = (MapFragment) fragment.findFragmentById(R.id.map);
mf.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap map) {
mMap = map;
// Do other setup activities here too, as described elsewhere in this tutorial.
AddPlacesFragmentPermissionsDispatcher.updateLocationUIWithCheck(this); //UpdateLocationUI
// Turn on the My Location layer and the related control on the map.
// Get the current location of the device and set the position of the map.
//getDeviceLocation();
AddPlacesFragmentPermissionsDispatcher.getDeviceLocationWithCheck(this);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d("CONNECTION CALLBACK","failed");
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d("CONNECTION CALLBACK","connected");
}
@Override
public void onConnectionSuspended(int i) {
Log.d("CONNECTION CALLBACK","suspended");
}
@NeedsPermission({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void updateLocationUI() {
if (mMap == null) {
Log.d("LOCATION","MAPS ARE NULL");
}else{
Log.d("LOCATION","updateLocationUI");
//noinspection MissingPermission
mMap.setMyLocationEnabled(true);
mLocationPermissionGranted = true;
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
AddPlacesFragmentPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
@OnShowRationale({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void updateLocationUIOnShowRationale(final PermissionRequest request) {
}
@OnPermissionDenied({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void updateLocationUIOnPermissionDenied() {
mMap.setMyLocationEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
@OnNeverAskAgain({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void updateLocationUIOnNeverAskAgain() {
}
@NeedsPermission({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void getDeviceLocation() {
mLastKnownLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
// Set the map's camera position to the current location of the device.
if (mCameraPosition != null) {
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
} else if (mLastKnownLocation != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
} else {
Log.d(TAG, "Current location is null. Using defaults.");
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
}
修改 所以我已经根据你的建议进行了一些更改,而且我不会在
中更长时间地收到错误SetMyLocationEnabled(true)
(尽管如果我在方法中移动它,我有时会得到它)
现在我在教程中有一些进展,我遇到了同样的问题。现在我在
中收到错误mLastKnownLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
并且真的不知道该做什么。代码编译,但在模拟器上运行它只是放大默认位置并在我的手机上运行应用程序崩溃
修改 在我的手机上运行它并在堆栈跟踪中发现异常(这次应用程序没有崩溃,但只显示了世界地图,没有任何我的位置信号或缩放到默认位置)
06-19 23:10:33.990 3728-6926/? D/LocationManagerService: request 3ab885c
fused Request[ACCURACY_BLOCK fused requested=+30m0s0ms fastest=+30m0s0ms] from com.samsung.voiceserviceplatform(10039)
06-19 23:10:34.083 3728-6926/? D/LocationManagerService: provider request: fused ProviderRequest[ON interval=+30m0s0ms]
06-19 23:10:34.084 4511-4511/? D/FusedRequestManager_FLP: manageLocationRequest, new fused request from com.samsung.voiceserviceplatform with 3ab885c , interval = 1800000 through LocationManagerService
06-19 23:10:34.126 4707-4936/? E/LocationClientHelper: exception when setting module id
java.lang.IllegalStateException: Unable to get current module info in ModuleManager created with non-module Context
at com.google.android.chimera.config.ModuleManager.getCurrentModule(:com.google.android.gms:1)
at tpn.a(:com.google.android.gms:17)
at tpr.b(:com.google.android.gms:13)
at too.a(:com.google.android.gms:4)
at kit.b(:com.google.android.gms:3)
at kjv.b(:com.google.android.gms:14)
at kks.b(:com.google.android.gms:7)
at kkk.b(:com.google.android.gms:25)
at txt.a(:com.google.android.gms:6)
at txs.b(:com.google.android.gms:1)
at txs.a(:com.google.android.gms:1)
at txs.a(:com.google.android.gms:15)
at tyc.a(:com.google.android.gms:8)
at ajuo.handleMessage(:com.google.android.gms:21)
at android.os.Handler.dispatchMessage(Handler.java:102)
at akwg.dispatchMessage(:com.google.android.gms:7)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
06-19 23:10:37.578 4707-27455/? W/ctxmgr: [AclManager]No 2 for (accnt=account#1348010355#, com.google.android.gms(10018):UserLocationProducer, vrsn=11055000, 0, 3pPkg = null , 3pMdlId = null , pid = 4707). Was: 2 for 1, account#1348010355#
06-19 23:10:37.580 4707-27455/? W/ctxmgr: [AclManager]No 2 for (accnt=account#-1563429706#, com.google.android.gms(10018):UserLocationProducer, vrsn=11055000, 0, 3pPkg = null , 3pMdlId = null , pid = 4707). Was: 2 for 1, account#-1563429706#
06-19 23:10:43.903 3728-4583/? D/LocationManagerService: getLastLocation: Request[POWER_NONE passive fastest=0 num=1]
06-19 23:10:43.968 3728-6921/? D/LocationManagerService: getLastLocation: Request[POWER_NONE passive fastest=0 num=1]
谢谢你或帮助我..
答案 0 :(得分:1)
应用程序异步获取地图,因此即使它在调用mf.getMapAsync(this)
后立即询问权限,该应用也可以加载地图并调用onMapReady
,通过mMap.setMyLocationEnabled(true)
调用您的方法,在用户有机会允许之前。我建议如下:
@Override
public void onMapReady(final GoogleMap map) {
mMap = map;
//Now that you have the map, request permission here.
getPermisions();
}
这是你应该如何处理的,因为你只在加载地图后才请求该位置。