0

지도 API를 사용하는 앱을 만들고 있습니다. 내가 원하는 것은 권한 팝업을 만드는 것입니다. 사용자가 허용을 클릭하면 현재 위치를 확대해야합니다. 이제 나는 그가 이것을 허락하지 않으려면 어떻게 될지 결정하고 싶다. 그가이 권한 맵을 거부하면 사용자 정의 위치로 확대해야하지만 거기에는 절대로 가면 토스트가 표시되지 않습니다. 여기지도 위치 세부 권한이 거부 됨 맞춤 위치로 이동

요청 허가 :

내지도 조각이처럼 보이는 여기

mapView.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
       ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) { 

      } else { 
       ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_LOCATION_REQUEST_CODE); 
      } 
      return; 
     } 

     mMap.setMyLocationEnabled(true); 
     LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 

     //Disable Map Toolbar: 
     mMap.getUiSettings().setMapToolbarEnabled(false); 

     // Get the name of the best provider and current location 
     String provider = null; 
     if (locationManager != null) { 
      provider = locationManager.getBestProvider(criteria, true); 
     } 
     // Get current location 
     Location myLocation = null; 
     if (locationManager != null) { 
      myLocation = locationManager.getLastKnownLocation(provider); 
     } 

     // Set default latitude and longitude to Greenwich 
     double latitude = 51.4825766; 
     double longitude = -0.0076589; 

     // Get latitude and longitude of the current location and a LatLng object 
     if (myLocation != null) { 
      latitude = myLocation.getLatitude(); 
      longitude = myLocation.getLongitude(); 
     } 

     CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(new LatLng(latitude, longitude)).zoom(14).build(); 
     mMap.animateCamera(CameraUpdateFactory 
       .newCameraPosition(cameraPosition)); 

} 

을 끄는 결과 :

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    if (requestCode == MY_LOCATION_REQUEST_CODE) { 
     if (permissions.length == 1 && 
       permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION && 
       grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != 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. 
       return; 
      } 
      mMap.setMyLocationEnabled(true); 
     } else { 
      // Permission was denied. Display an error message. 
      double latitude = 51.4825766; 
      double longitude = -0.0076589; 
      Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();   CameraPosition cameraPosition = new CameraPosition.Builder() 
          .target(new LatLng(latitude, longitude)).zoom(14).build(); 
        mMap.animateCamera(CameraUpdateFactory 
          .newCameraPosition(cameraPosition)); 
     } 
    } 
} 

답변

0

부모 액티비티에 권한을 부여하여 작동하게합니다.