|
1 | 1 | package com.pulkit4tech.privy;
|
2 | 2 |
|
| 3 | +import android.Manifest; |
3 | 4 | import android.content.Context;
|
4 | 5 | import android.content.pm.PackageManager;
|
5 | 6 | import android.os.Bundle;
|
| 7 | +import android.support.annotation.NonNull; |
6 | 8 | import android.support.v4.app.ActivityCompat;
|
7 | 9 | import android.support.v7.app.ActionBarActivity;
|
8 | 10 | import android.util.Log;
|
|
18 | 20 | import com.google.android.gms.maps.model.LatLng;
|
19 | 21 | import com.google.android.gms.maps.model.Marker;
|
20 | 22 | import com.google.android.gms.maps.model.MarkerOptions;
|
21 |
| -import com.pulkit4tech.privy.Utilities.LocationServices; |
22 | 23 | import com.pulkit4tech.privy.data.LocationData;
|
| 24 | +import com.pulkit4tech.privy.utilities.LocationServices; |
23 | 25 |
|
24 |
| -import static com.pulkit4tech.privy.MainActivity.DEBUG; |
| 26 | +import static com.pulkit4tech.privy.constants.Constants.DEBUG; |
| 27 | +import static com.pulkit4tech.privy.constants.Constants.CAMERA_ANIMATION_DURATION; |
| 28 | +import static com.pulkit4tech.privy.constants.Constants.MY_PERMISSIONS_REQUEST_FINE_LOCATIONS; |
25 | 29 |
|
26 | 30 | public class PrivyMapsActivity extends ActionBarActivity implements OnMapReadyCallback {
|
27 | 31 |
|
28 | 32 | private GoogleMap mMap;
|
29 | 33 | private Context mContext;
|
30 | 34 | private Marker myLocationMarker;
|
31 | 35 | private CameraPosition MY_LOCATION_CAMERA_POS;
|
32 |
| - private static final int CAMERA_ANIMATION_DURATION = 2500; |
33 | 36 |
|
34 | 37 | // My Location
|
35 | 38 | private LocationData myLocationData;
|
@@ -79,12 +82,17 @@ private void addMarkers() {
|
79 | 82 |
|
80 | 83 | private void setUpMapInfo() {
|
81 | 84 |
|
82 |
| - if(!checkLocationEnabledPermission()) |
83 |
| - return; |
| 85 | + if(!checkLocationEnabledPermission()){ |
| 86 | + ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSIONS_REQUEST_FINE_LOCATIONS); |
| 87 | + }else { |
| 88 | + setUpMyLocationMarker(); |
| 89 | + } |
84 | 90 |
|
| 91 | + } |
85 | 92 |
|
| 93 | + private void setUpMyLocationMarker() { |
| 94 | + mMap.setMyLocationEnabled(true); |
86 | 95 | getMyCurrentLocation();
|
87 |
| - |
88 | 96 | }
|
89 | 97 |
|
90 | 98 | private void getMyCurrentLocation(){
|
@@ -137,18 +145,25 @@ private void changeCamera(CameraUpdate update, GoogleMap.CancelableCallback call
|
137 | 145 | }
|
138 | 146 |
|
139 | 147 | private boolean checkLocationEnabledPermission() {
|
140 |
| - if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
141 |
| - // TODO: Consider calling |
142 |
| - // ActivityCompat#requestPermissions |
143 |
| - // here to request the missing permissions, and then overriding |
144 |
| - // public void onRequestPermissionsResult(int requestCode, String[] permissions, |
145 |
| - // int[] grantResults) |
146 |
| - // to handle the case where the user grants the permission. See the documentation |
147 |
| - // for ActivityCompat#requestPermissions for more details. |
148 |
| - |
149 |
| - return false; |
| 148 | + return ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 153 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 154 | + |
| 155 | + switch (requestCode){ |
| 156 | + case MY_PERMISSIONS_REQUEST_FINE_LOCATIONS: |
| 157 | + if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ |
| 158 | + setUpMyLocationMarker(); |
| 159 | + } |
| 160 | + else { |
| 161 | + Toast.makeText(mContext,"Please give permission for location",Toast.LENGTH_SHORT).show(); |
| 162 | + } |
| 163 | + break; |
| 164 | + |
| 165 | + default: |
| 166 | + Log.d(DEBUG,"Some other request code: " + requestCode); |
150 | 167 | }
|
151 |
| - mMap.setMyLocationEnabled(true); |
152 |
| - return true; |
153 | 168 | }
|
154 | 169 | }
|
0 commit comments