Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. public void onMapReady(GoogleMap googleMap) {
  2. mMap = googleMap;
  3.  
  4. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  5. // Permission is not yet available and needs to be asked for
  6. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
  7. // We provide an additional rationale to the user if the permission was not granted
  8. // and the user would benefit from additional context for the use of the permission.
  9. // For example if the user has previously denied the permission.
  10. new AlertDialog.Builder(Map.this)
  11. .setMessage("To show the location the permission is needed")
  12. .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  13. @Override
  14. public void onClick(DialogInterface dialog, int which) {
  15. ActivityCompat.requestPermissions(Map.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
  16. }
  17. })
  18. .setNegativeButton("Cancel", null)
  19. .create()
  20. .show();
  21. } else {
  22. // Permission has not been granted yet. Request it directly.
  23. ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
  24. }
  25.  
  26. } else {
  27. // Permission is already available
  28. setUpMarker();
  29. }
  30.  
  31. }
  32.  
  33.  
  34. @Override
  35. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  36. switch (requestCode) {
  37. case LOCATION_PERMISSION_REQUEST_ID: {
  38. // If request is cancelled, the result arrays are empty.
  39. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  40. // The permission request was granted, we set up the marker
  41. setUpMarker();
  42. } else {
  43. // The permission request was denied, we make the user aware of why the location is not shown
  44. Toast.makeText(this,"Since the permission wasn't granted we can't show the location",Toast.LENGTH_LONG).show();
  45. }
  46. return;
  47. }
  48.  
  49. }
  50. }
  51.  
  52. 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'.
  53.  
  54. private void setUpMarker() {
  55. //HERE I GET THE ERROR
  56. mMap.setMyLocationEnabled(true);
  57.  
  58. if(!getIntent().getExtras().isEmpty()) {
  59. // Latitude and longitude was retrieved successfully, set up marker
  60.  
  61. ...
  62.  
  63. } else {
  64. // Latitude and longitude was not retrieved, zoom to current location
  65.  
  66. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  67. Criteria criteria = new Criteria();
  68.  
  69. // AND HERE I GET THE ERROR TOO
  70. Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
  71. if (location != null) {
  72. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
  73. new LatLng(location.getLatitude(), location.getLongitude()), 13));
  74.  
  75. CameraPosition cameraPosition = new CameraPosition.Builder()
  76. .target(new LatLng(location.getLatitude(), location.getLongitude()))
  77. .zoom(17)
  78. .bearing(90)
  79. .tilt(40)
  80. .build();
  81. mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  82.  
  83. }
  84.  
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement