1
+ public class MyLocationListener
2
+ implements GoogleApiClient .ConnectionCallbacks ,
3
+ GoogleApiClient .OnConnectionFailedListener , com .google .android .gms .location .LocationListener
4
+ {
5
+ private final String TAG = "LOC_RECURRING_SAMPLE" ;
6
+
7
+ // Constants that define how often location updates will be delivered
8
+ private final long LOC_UPDATE_INTERVAL = 10000 ; // 10s in milliseconds
9
+ private final long LOC_FASTEST_UPDATE = 5000 ; // 5s in milliseconds
10
+
11
+ protected GoogleApiClient mGoogleApiClient ;
12
+ protected LocationRequest mLocRequest ;
13
+ public Location mCurLocation ;
14
+
15
+ Context context ;
16
+ public MyLocationListener ( Context context ) {
17
+ this .context =context ;
18
+ mCurLocation = null ;
19
+
20
+ // build the Play Services client object
21
+ mGoogleApiClient = new GoogleApiClient .Builder (context )
22
+ .addConnectionCallbacks (this )
23
+ .addOnConnectionFailedListener (this )
24
+ .addApi (LocationServices .API )
25
+ .build ();
26
+ mLocRequest =new LocationRequest ();
27
+ mLocRequest .setInterval (LOC_UPDATE_INTERVAL );
28
+ mLocRequest .setFastestInterval (LOC_FASTEST_UPDATE );
29
+ mLocRequest .setPriority (LocationRequest .PRIORITY_HIGH_ACCURACY );
30
+ // TODO: create the LocationRequest we'll use for location updates
31
+ onStart ();
32
+ }
33
+
34
+
35
+ public void startLocationUpdates () {
36
+ // TODO: start the location updates
37
+
38
+ LocationServices .FusedLocationApi .requestLocationUpdates (
39
+ mGoogleApiClient ,mLocRequest ,this );
40
+
41
+
42
+ }
43
+
44
+ public void stopLocationUpdates () {
45
+ // TODO: stop the updates
46
+
47
+ LocationServices .FusedLocationApi .removeLocationUpdates (
48
+ mGoogleApiClient , this );
49
+
50
+
51
+ }
52
+
53
+ protected void updateUI () {
54
+ // take the lat and long of the current location object and add it to the list
55
+ if (mCurLocation != null ) {
56
+ String lat = String .format ("Lat: %f\n " , mCurLocation .getLatitude ());
57
+ String lon = String .format ("Lon: %f\n " , mCurLocation .getLongitude ());
58
+
59
+
60
+ }
61
+ }
62
+
63
+ protected void initializeUI () {
64
+ // start by getting the last known location as a starting point
65
+ if (mCurLocation == null ) {
66
+ mCurLocation = LocationServices .FusedLocationApi .getLastLocation (mGoogleApiClient );
67
+
68
+ updateUI ();
69
+
70
+
71
+ }
72
+
73
+
74
+ }
75
+
76
+ /**
77
+ * Called to handle the button clicks in the view
78
+ */
79
+
80
+ public void onClick (int id ) {
81
+ switch (id ){
82
+ case 1 :
83
+ startLocationUpdates ();
84
+ break ;
85
+ case 0 :
86
+ stopLocationUpdates ();
87
+ break ;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Called by Play Services when the user's location changes
93
+ */
94
+ @ Override
95
+ public void onLocationChanged (Location loc ) {
96
+ mCurLocation = loc ;
97
+ LocationService .location =loc ;
98
+ updateUI ();
99
+ }
100
+
101
+
102
+
103
+
104
+
105
+
106
+ /**
107
+ * Google Play Services Lifecycle methods
108
+ */
109
+ @ Override
110
+ public void onConnected (Bundle connectionHint ) {
111
+
112
+ initializeUI ();
113
+ startLocationUpdates ();
114
+ }
115
+
116
+ @ Override
117
+ public void onConnectionFailed (ConnectionResult result ) {
118
+ Log .d (TAG , "Connection failed: ConnectionResult.getErrorCode() = " + result .getErrorCode ());
119
+ }
120
+
121
+ @ Override
122
+ public void onConnectionSuspended (int cause ) {
123
+ Log .d (TAG , "Connection was suspended for some reason" );
124
+ mGoogleApiClient .connect ();
125
+ }
126
+
127
+ /**
128
+ * Activity lifecycle events
129
+ */
130
+ public void onStart () {
131
+
132
+ mGoogleApiClient .connect ();
133
+ }
134
+
135
+ public void onStop () {
136
+ mGoogleApiClient .disconnect ();
137
+
138
+ }
139
+
140
+ }
0 commit comments