Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // Permission is not yet available and needs to be asked for
- if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
- // We provide an additional rationale to the user if the permission was not granted
- // and the user would benefit from additional context for the use of the permission.
- // For example if the user has previously denied the permission.
- new AlertDialog.Builder(Map.this)
- .setMessage("To show the location the permission is needed")
- .setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- ActivityCompat.requestPermissions(Map.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
- }
- })
- .setNegativeButton("Cancel", null)
- .create()
- .show();
- } else {
- // Permission has not been granted yet. Request it directly.
- ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
- }
- } else {
- // Permission is already available
- setUpMarker();
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case LOCATION_PERMISSION_REQUEST_ID: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // The permission request was granted, we set up the marker
- setUpMarker();
- } else {
- // The permission request was denied, we make the user aware of why the location is not shown
- Toast.makeText(this,"Since the permission wasn't granted we can't show the location",Toast.LENGTH_LONG).show();
- }
- return;
- }
- }
- }
- Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with 'checkPermission') or explicitly handle a potential 'SecurityException'.
- private void setUpMarker() {
- //HERE I GET THE ERROR
- mMap.setMyLocationEnabled(true);
- if(!getIntent().getExtras().isEmpty()) {
- // Latitude and longitude was retrieved successfully, set up marker
- ...
- } else {
- // Latitude and longitude was not retrieved, zoom to current location
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- Criteria criteria = new Criteria();
- // AND HERE I GET THE ERROR TOO
- Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
- if (location != null) {
- mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
- new LatLng(location.getLatitude(), location.getLongitude()), 13));
- CameraPosition cameraPosition = new CameraPosition.Builder()
- .target(new LatLng(location.getLatitude(), location.getLongitude()))
- .zoom(17)
- .bearing(90)
- .tilt(40)
- .build();
- mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement