1

一切正常工作在其他機器人,但在6.0 MyLocationButton不會做任何事情。這是我的代碼Android:我的位置按鈕不適用於Android 6.0

googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
googleMap.setMyLocationEnabled(true); 
googleMap 
       .setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { 

        @Override 
        public boolean onMyLocationButtonClick() { 

         Location location = googleMap.getMyLocation(); 
         if (location == null) { 
          checkLocationSettings(); 
          return true; 
         } 

         LatLng locationLatLng = new LatLng(location 
           .getLatitude(), location.getLongitude()); 
         setMarkerToMap(locationLatLng); 
         //other staff 
         return false; 
}); 

protected void checkLocationSettings() { 
     PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi 
       .checkLocationSettings(mGoogleApiClient, 
         mLocationSettingsRequest); 
     result.setResultCallback(this); 
    } 

@Override 
    public void onResult(LocationSettingsResult locationSettingsResult) { 
     final Status status = locationSettingsResult.getStatus(); 
     switch (status.getStatusCode()) { 
      case LocationSettingsStatusCodes.SUCCESS: 

       break; 
      case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
       try { 
        status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS); 
       } catch (IntentSender.SendIntentException e) { 

       } 
       break; 

     } 
    } 

什麼會導致此問題?我想它必須做一些新的權限(在Android 6.0中),但我無法弄清楚。

+0

您是否嘗試過檢查裏面OnClickListener ACCESS_FINE_LOCATION許可verifyStoragePermissions()方法,並呼籲在onCreateView()活動的 ? – noktigula

回答

0

請參考這些鏈接並在運行時添加權限。

https://blog.xamarin.com/requesting-runtime-permissions-in-android- 棉花糖/

或嘗試這些

創建

private static String[] PERMISSIONS_STORAGE = { 
    Manifest.Permission.AccessCoarseLocation, 
    Manifest.Permission.AccessFineLocation}; 
public static void verifyStoragePermissions(Activity activity) 
int permission = ActivityCompat.checkSelfPermission(activity, 
      Manifest.permission.AccessCoarseLocation); 
if (permission != PackageManager.PERMISSION_GRANTED) 
ActivityCompat.requestPermissions(
      activity, 
      PERMISSIONS_STORAGE, 
      1001 // pass any request code 
    ); 
} 
相關問題