|
| 1 | +package com.hellohasan.sixteenthclass; |
| 2 | + |
| 3 | +import android.content.pm.PackageManager; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.annotation.NonNull; |
| 6 | +import android.support.v4.app.ActivityCompat; |
| 7 | +import android.support.v4.app.FragmentActivity; |
| 8 | +import android.support.v4.content.ContextCompat; |
| 9 | + |
| 10 | +import com.google.android.gms.maps.CameraUpdateFactory; |
| 11 | +import com.google.android.gms.maps.GoogleMap; |
| 12 | +import com.google.android.gms.maps.OnMapReadyCallback; |
| 13 | +import com.google.android.gms.maps.SupportMapFragment; |
| 14 | +import com.google.android.gms.maps.model.LatLng; |
| 15 | +import com.google.android.gms.maps.model.MarkerOptions; |
| 16 | +import com.orhanobut.logger.AndroidLogAdapter; |
| 17 | +import com.orhanobut.logger.Logger; |
| 18 | + |
| 19 | +public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { |
| 20 | + |
| 21 | + private GoogleMap mMap; |
| 22 | + private boolean mLocationPermissionGranted = false; |
| 23 | + private final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1001; |
| 24 | + |
| 25 | + @Override |
| 26 | + protected void onCreate(Bundle savedInstanceState) { |
| 27 | + super.onCreate(savedInstanceState); |
| 28 | + setContentView(R.layout.activity_maps); |
| 29 | + Logger.addLogAdapter(new AndroidLogAdapter()); |
| 30 | + // Obtain the SupportMapFragment and get notified when the map is ready to be used. |
| 31 | + SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() |
| 32 | + .findFragmentById(R.id.map); |
| 33 | + mapFragment.getMapAsync(this); |
| 34 | + |
| 35 | + getLocationPermission(); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * Manipulates the map once available. |
| 42 | + * This callback is triggered when the map is ready to be used. |
| 43 | + * This is where we can add markers or lines, add listeners or move the camera. In this case, |
| 44 | + * we just add a marker near Sydney, Australia. |
| 45 | + * If Google Play services is not installed on the device, the user will be prompted to install |
| 46 | + * it inside the SupportMapFragment. This method will only be triggered once the user has |
| 47 | + * installed Google Play services and returned to the app. |
| 48 | + */ |
| 49 | + @Override |
| 50 | + public void onMapReady(GoogleMap googleMap) { |
| 51 | + mMap = googleMap; |
| 52 | + |
| 53 | + // Add a marker in Sydney and move the camera |
| 54 | + LatLng ahmadNagar = new LatLng(23.794469, 90.358917); |
| 55 | + mMap.addMarker(new MarkerOptions().position(ahmadNagar).title("Marker in Ahmad Nagar")); |
| 56 | + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ahmadNagar, 15)); |
| 57 | + |
| 58 | + updateLocationUI(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onRequestPermissionsResult(int requestCode, |
| 63 | + @NonNull String permissions[], |
| 64 | + @NonNull int[] grantResults) { |
| 65 | + mLocationPermissionGranted = false; |
| 66 | + switch (requestCode) { |
| 67 | + case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { |
| 68 | + // If request is cancelled, the result arrays are empty. |
| 69 | + if (grantResults.length > 0 |
| 70 | + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 71 | + mLocationPermissionGranted = true; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + updateLocationUI(); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + private void updateLocationUI() { |
| 80 | + if (mMap == null) { |
| 81 | + Logger.d("Map is null"); |
| 82 | + return; |
| 83 | + } |
| 84 | + try { |
| 85 | + if (mLocationPermissionGranted) { |
| 86 | + mMap.setMyLocationEnabled(true); |
| 87 | + mMap.getUiSettings().setMyLocationButtonEnabled(true); |
| 88 | + } else { |
| 89 | + mMap.setMyLocationEnabled(false); |
| 90 | + mMap.getUiSettings().setMyLocationButtonEnabled(false); |
| 91 | +// mLastKnownLocation = null; |
| 92 | + getLocationPermission(); |
| 93 | + } |
| 94 | + } catch (SecurityException e) { |
| 95 | + Logger.d(e.getMessage()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void getLocationPermission() { |
| 100 | + /* |
| 101 | + * Request location permission, so that we can get the location of the |
| 102 | + * device. The result of the permission request is handled by a callback, |
| 103 | + * onRequestPermissionsResult. |
| 104 | + */ |
| 105 | + if (ContextCompat.checkSelfPermission(this.getApplicationContext(), |
| 106 | + android.Manifest.permission.ACCESS_FINE_LOCATION) |
| 107 | + == PackageManager.PERMISSION_GRANTED) { |
| 108 | + mLocationPermissionGranted = true; |
| 109 | + } else { |
| 110 | + ActivityCompat.requestPermissions(this, |
| 111 | + new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, |
| 112 | + PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments