2
2
3
3
import android .Manifest ;
4
4
import android .content .Context ;
5
+ import android .content .Intent ;
5
6
import android .content .pm .PackageManager ;
7
+ import android .location .Location ;
8
+ import android .location .LocationManager ;
6
9
import android .os .Bundle ;
10
+ import android .provider .Settings ;
11
+ import android .support .annotation .NonNull ;
7
12
import android .support .annotation .Nullable ;
8
13
import android .support .design .widget .CoordinatorLayout ;
9
14
import android .support .design .widget .Snackbar ;
13
18
import android .view .LayoutInflater ;
14
19
import android .view .View ;
15
20
import android .view .ViewGroup ;
16
- import android .widget .Toast ;
17
21
22
+ import com .google .android .gms .common .ConnectionResult ;
23
+ import com .google .android .gms .common .api .GoogleApiClient ;
24
+ import com .google .android .gms .location .LocationListener ;
25
+ import com .google .android .gms .location .LocationRequest ;
26
+ import com .google .android .gms .location .LocationServices ;
18
27
import com .google .android .gms .maps .CameraUpdate ;
19
28
import com .google .android .gms .maps .CameraUpdateFactory ;
20
29
import com .google .android .gms .maps .GoogleMap ;
23
32
import com .google .android .gms .maps .model .CameraPosition ;
24
33
import com .google .android .gms .maps .model .LatLng ;
25
34
import com .pulkit4tech .privy .R ;
26
- import com .pulkit4tech .privy .data .LocationData ;
27
35
import com .pulkit4tech .privy .data .json .MarkerData ;
28
- import com .pulkit4tech .privy .utilities .LocationServices ;
29
36
import com .pulkit4tech .privy .utilities .NetworkRequest ;
30
37
31
38
import java .util .HashMap ;
32
39
40
+ import static android .content .Context .LOCATION_SERVICE ;
33
41
import static com .pulkit4tech .privy .constants .Constants .DEBUG ;
34
42
import static com .pulkit4tech .privy .constants .Constants .CAMERA_ANIMATION_DURATION ;
35
43
import static com .pulkit4tech .privy .constants .Constants .MY_PERMISSIONS_REQUEST_FINE_LOCATIONS ;
36
44
37
- public class PrivyMapsFragment extends Fragment implements OnMapReadyCallback {
45
+ public class PrivyMapsFragment extends Fragment implements OnMapReadyCallback , GoogleApiClient . ConnectionCallbacks , GoogleApiClient . OnConnectionFailedListener , LocationListener {
38
46
39
47
private GoogleMap mMap ;
40
48
private Context mContext ;
41
49
private CameraPosition MY_LOCATION_CAMERA_POS ;
42
50
private HashMap <String , MarkerData > universalMarkers ;
51
+ private GoogleApiClient mGoogleApiClient ;
52
+ private LocationRequest mLocationRequest ;
53
+ private static int UPDATE_INTERVAL = 10000 ; // 10 sec
54
+ private static int FATEST_INTERVAL = 5000 ; // 5 sec
55
+ private static int DISPLACEMENT = 10 ; // 10 meters
43
56
44
57
// My location
45
- private LocationData myLocationData ;
58
+ private Location myLocationData ;
59
+
60
+ @ Override
61
+ public void onStop () {
62
+ super .onStop ();
63
+ mGoogleApiClient .disconnect ();
64
+ }
46
65
47
66
@ Nullable
48
67
@ Override
@@ -52,6 +71,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
52
71
View mView = inflater .inflate (R .layout .activity_privy_maps , container , false );
53
72
mContext = getActivity ();
54
73
universalMarkers = new HashMap <>();
74
+ setUpGoogleApiClient ();
55
75
56
76
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
57
77
SupportMapFragment mapFragment = (SupportMapFragment ) getChildFragmentManager ()
@@ -61,6 +81,22 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
61
81
return mView ;
62
82
}
63
83
84
+ private void setUpGoogleApiClient () {
85
+ if (mGoogleApiClient == null ) {
86
+ mGoogleApiClient = new GoogleApiClient .Builder (getContext ())
87
+ .addConnectionCallbacks (this )
88
+ .addOnConnectionFailedListener (this )
89
+ .addApi (LocationServices .API )
90
+ .build ();
91
+ }
92
+ }
93
+
94
+ @ Override
95
+ public void onStart () {
96
+ super .onStart ();
97
+ mGoogleApiClient .connect ();
98
+ }
99
+
64
100
/**
65
101
* Manipulates the map once available.
66
102
* This callback is triggered when the map is ready to be used.
@@ -80,12 +116,11 @@ public void onMapReady(GoogleMap googleMap) {
80
116
private void addMarkers () {
81
117
82
118
if (myLocationData == null ) {
83
- LocationServices locationService = new LocationServices (mContext );
84
- myLocationData = locationService .getCurrentLocation ();
119
+ myLocationData = getCurrentLocation ();
85
120
}
86
121
87
122
if (myLocationData != null )
88
- markNearbyPrivys (myLocationData .getLatLng ( ));
123
+ markNearbyPrivys (new LatLng ( myLocationData .getLatitude (), myLocationData . getLongitude () ));
89
124
90
125
// Add a test marker in Delhi and move the camera
91
126
// LatLng delhi = new LatLng(28.633011, 77.219373);
@@ -119,12 +154,11 @@ public boolean onMyLocationButtonClick() {
119
154
}
120
155
121
156
private void getMyCurrentLocation () {
122
- LocationServices locationService = new LocationServices (mContext );
123
- myLocationData = locationService .getCurrentLocation ();
157
+ myLocationData = getCurrentLocation ();
124
158
if (myLocationData != null ) {
125
159
126
160
MY_LOCATION_CAMERA_POS = new CameraPosition .Builder ()
127
- .target (myLocationData .getLatLng ( ))
161
+ .target (new LatLng ( myLocationData .getLatitude (), myLocationData . getLongitude () ))
128
162
.zoom (15.0f )
129
163
.bearing (0 )
130
164
.tilt (25 )
@@ -134,20 +168,24 @@ private void getMyCurrentLocation() {
134
168
moveCameraToMyLocation ();
135
169
addMarkers ();
136
170
137
- Log .d (DEBUG , myLocationData .getLatLng ().toString ());
171
+ Log .d (DEBUG , new LatLng (myLocationData .getLatitude (), myLocationData .getLongitude ()).toString ());
172
+ } else {
173
+ Log .d (DEBUG , "Can't retrieve location at the moment." );
174
+ if (!checkGPSon ())
175
+ promptToEnableGPS ();
138
176
}
139
177
}
140
178
141
179
private void moveCameraToMyLocation () {
142
180
changeCamera (CameraUpdateFactory .newCameraPosition (MY_LOCATION_CAMERA_POS ), new GoogleMap .CancelableCallback () {
143
181
@ Override
144
182
public void onFinish () {
145
- snackMsg ( " Animation Finished " );
183
+ Log . d ( DEBUG , "moveCameraToMyLocation: Camera Animation finished " );
146
184
}
147
185
148
186
@ Override
149
187
public void onCancel () {
150
- Toast . makeText ( mContext , "Animation Canceled" , Toast . LENGTH_SHORT ). show ( );
188
+ Log . d ( DEBUG , "moveCameraToMyLocation: Camera Animation Cancelled" );
151
189
}
152
190
});
153
191
@@ -168,4 +206,88 @@ private void markNearbyPrivys(LatLng myLocation) {
168
206
private void snackMsg (String msg ) {
169
207
Snackbar .make ((CoordinatorLayout ) getActivity ().findViewById (R .id .coordinator_layout ), msg , Snackbar .LENGTH_LONG ).show ();
170
208
}
209
+
210
+ @ Override
211
+ public void onConnected (@ Nullable Bundle bundle ) {
212
+ myLocationData = com .google .android .gms .location .LocationServices .FusedLocationApi .getLastLocation (
213
+ mGoogleApiClient );
214
+ if (myLocationData == null ) {
215
+ if (!checkGPSon ()) {
216
+ promptToEnableGPS ();
217
+ }
218
+ } else {
219
+ getMyCurrentLocation ();
220
+ }
221
+
222
+ startLocationUpdates ();
223
+ }
224
+
225
+ private Location getCurrentLocation () {
226
+ if (myLocationData != null )
227
+ return myLocationData ;
228
+ Location location = com .google .android .gms .location .LocationServices .FusedLocationApi .getLastLocation (
229
+ mGoogleApiClient );
230
+ if (location == null && !checkGPSon ())
231
+ promptToEnableGPS ();
232
+
233
+ return location ;
234
+
235
+ }
236
+
237
+ @ Override
238
+ public void onConnectionSuspended (int i ) {
239
+ stopLocationUpdates ();
240
+ }
241
+
242
+ @ Override
243
+ public void onConnectionFailed (@ NonNull ConnectionResult connectionResult ) {
244
+ snackMsg ("Can't connect to Google Api Client" );
245
+ }
246
+
247
+
248
+ private void snackMsgWithAction (String msg ) {
249
+ Snackbar snackbar = Snackbar .make (getActivity ().findViewById (R .id .coordinator_layout ), msg , Snackbar .LENGTH_LONG );
250
+ snackbar .setAction (R .string .turn_on , new View .OnClickListener () {
251
+ @ Override
252
+ public void onClick (View view ) {
253
+ // start intent to turn on GPS
254
+ Intent onGPS = new Intent (Settings .ACTION_LOCATION_SOURCE_SETTINGS );
255
+ mContext .startActivity (onGPS );
256
+ }
257
+ });
258
+ snackbar .show ();
259
+ }
260
+
261
+ private void promptToEnableGPS () {
262
+ snackMsgWithAction (mContext .getString (R .string .enable_gps_msg ));
263
+ }
264
+
265
+ private boolean checkGPSon () {
266
+ LocationManager locationManager = (LocationManager ) mContext .getSystemService (LOCATION_SERVICE );
267
+ return locationManager .isProviderEnabled (LocationManager .GPS_PROVIDER );
268
+ }
269
+
270
+ protected void createLocationRequest () {
271
+ mLocationRequest = new LocationRequest ();
272
+ mLocationRequest .setInterval (UPDATE_INTERVAL );
273
+ mLocationRequest .setFastestInterval (FATEST_INTERVAL );
274
+ mLocationRequest .setPriority (LocationRequest .PRIORITY_HIGH_ACCURACY );
275
+ mLocationRequest .setSmallestDisplacement (DISPLACEMENT ); // 10 meters
276
+ }
277
+
278
+ private void startLocationUpdates () {
279
+ createLocationRequest ();
280
+ LocationServices .FusedLocationApi .requestLocationUpdates (
281
+ mGoogleApiClient , mLocationRequest , this );
282
+ }
283
+
284
+ private void stopLocationUpdates () {
285
+ LocationServices .FusedLocationApi .removeLocationUpdates (
286
+ mGoogleApiClient , this );
287
+ }
288
+
289
+ @ Override
290
+ public void onLocationChanged (Location location ) {
291
+ getMyCurrentLocation ();
292
+ }
171
293
}
0 commit comments