22
33import android .Manifest ;
44import android .content .Context ;
5+ import android .content .Intent ;
56import android .content .pm .PackageManager ;
7+ import android .location .Location ;
8+ import android .location .LocationManager ;
69import android .os .Bundle ;
10+ import android .provider .Settings ;
11+ import android .support .annotation .NonNull ;
712import android .support .annotation .Nullable ;
813import android .support .design .widget .CoordinatorLayout ;
914import android .support .design .widget .Snackbar ;
1318import android .view .LayoutInflater ;
1419import android .view .View ;
1520import android .view .ViewGroup ;
16- import android .widget .Toast ;
1721
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 ;
1827import com .google .android .gms .maps .CameraUpdate ;
1928import com .google .android .gms .maps .CameraUpdateFactory ;
2029import com .google .android .gms .maps .GoogleMap ;
2332import com .google .android .gms .maps .model .CameraPosition ;
2433import com .google .android .gms .maps .model .LatLng ;
2534import com .pulkit4tech .privy .R ;
26- import com .pulkit4tech .privy .data .LocationData ;
2735import com .pulkit4tech .privy .data .json .MarkerData ;
28- import com .pulkit4tech .privy .utilities .LocationServices ;
2936import com .pulkit4tech .privy .utilities .NetworkRequest ;
3037
3138import java .util .HashMap ;
3239
40+ import static android .content .Context .LOCATION_SERVICE ;
3341import static com .pulkit4tech .privy .constants .Constants .DEBUG ;
3442import static com .pulkit4tech .privy .constants .Constants .CAMERA_ANIMATION_DURATION ;
3543import static com .pulkit4tech .privy .constants .Constants .MY_PERMISSIONS_REQUEST_FINE_LOCATIONS ;
3644
37- public class PrivyMapsFragment extends Fragment implements OnMapReadyCallback {
45+ public class PrivyMapsFragment extends Fragment implements OnMapReadyCallback , GoogleApiClient . ConnectionCallbacks , GoogleApiClient . OnConnectionFailedListener , LocationListener {
3846
3947 private GoogleMap mMap ;
4048 private Context mContext ;
4149 private CameraPosition MY_LOCATION_CAMERA_POS ;
4250 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
4356
4457 // My location
45- private LocationData myLocationData ;
58+ private Location myLocationData ;
59+
60+ @ Override
61+ public void onStop () {
62+ super .onStop ();
63+ mGoogleApiClient .disconnect ();
64+ }
4665
4766 @ Nullable
4867 @ Override
@@ -52,6 +71,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
5271 View mView = inflater .inflate (R .layout .activity_privy_maps , container , false );
5372 mContext = getActivity ();
5473 universalMarkers = new HashMap <>();
74+ setUpGoogleApiClient ();
5575
5676 // Obtain the SupportMapFragment and get notified when the map is ready to be used.
5777 SupportMapFragment mapFragment = (SupportMapFragment ) getChildFragmentManager ()
@@ -61,6 +81,22 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
6181 return mView ;
6282 }
6383
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+
64100 /**
65101 * Manipulates the map once available.
66102 * This callback is triggered when the map is ready to be used.
@@ -80,12 +116,11 @@ public void onMapReady(GoogleMap googleMap) {
80116 private void addMarkers () {
81117
82118 if (myLocationData == null ) {
83- LocationServices locationService = new LocationServices (mContext );
84- myLocationData = locationService .getCurrentLocation ();
119+ myLocationData = getCurrentLocation ();
85120 }
86121
87122 if (myLocationData != null )
88- markNearbyPrivys (myLocationData .getLatLng ( ));
123+ markNearbyPrivys (new LatLng ( myLocationData .getLatitude (), myLocationData . getLongitude () ));
89124
90125 // Add a test marker in Delhi and move the camera
91126// LatLng delhi = new LatLng(28.633011, 77.219373);
@@ -119,12 +154,11 @@ public boolean onMyLocationButtonClick() {
119154 }
120155
121156 private void getMyCurrentLocation () {
122- LocationServices locationService = new LocationServices (mContext );
123- myLocationData = locationService .getCurrentLocation ();
157+ myLocationData = getCurrentLocation ();
124158 if (myLocationData != null ) {
125159
126160 MY_LOCATION_CAMERA_POS = new CameraPosition .Builder ()
127- .target (myLocationData .getLatLng ( ))
161+ .target (new LatLng ( myLocationData .getLatitude (), myLocationData . getLongitude () ))
128162 .zoom (15.0f )
129163 .bearing (0 )
130164 .tilt (25 )
@@ -134,20 +168,24 @@ private void getMyCurrentLocation() {
134168 moveCameraToMyLocation ();
135169 addMarkers ();
136170
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 ();
138176 }
139177 }
140178
141179 private void moveCameraToMyLocation () {
142180 changeCamera (CameraUpdateFactory .newCameraPosition (MY_LOCATION_CAMERA_POS ), new GoogleMap .CancelableCallback () {
143181 @ Override
144182 public void onFinish () {
145- snackMsg ( " Animation Finished " );
183+ Log . d ( DEBUG , "moveCameraToMyLocation: Camera Animation finished " );
146184 }
147185
148186 @ Override
149187 public void onCancel () {
150- Toast . makeText ( mContext , "Animation Canceled" , Toast . LENGTH_SHORT ). show ( );
188+ Log . d ( DEBUG , "moveCameraToMyLocation: Camera Animation Cancelled" );
151189 }
152190 });
153191
@@ -168,4 +206,88 @@ private void markNearbyPrivys(LatLng myLocation) {
168206 private void snackMsg (String msg ) {
169207 Snackbar .make ((CoordinatorLayout ) getActivity ().findViewById (R .id .coordinator_layout ), msg , Snackbar .LENGTH_LONG ).show ();
170208 }
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+ }
171293}
0 commit comments