@@ -29,6 +29,7 @@ public final class LocationUtils {
29
29
private static OnLocationChangeListener mListener ;
30
30
private static MyLocationListener myLocationListener ;
31
31
private static LocationManager mLocationManager ;
32
+ private static final int TWO_MINUTES = 1000 * 60 * 2 ;
32
33
33
34
private LocationUtils () {
34
35
throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -183,6 +184,60 @@ public static String getStreet(double latitude, double longitude) {
183
184
Address address = getAddress (latitude , longitude );
184
185
return address == null ? "unknown" : address .getAddressLine (0 );
185
186
}
187
+
188
+ /** Determines whether one Location reading is better than the current Location fix
189
+ * @param newLocation The new Location that you want to evaluate
190
+ * @param currentBestLocation The current Location fix, to which you want to compare the new one
191
+ */
192
+ static boolean isBetterLocation (Location newLocation , Location currentBestLocation ) {
193
+ if (currentBestLocation == null ) {
194
+ // A new location is always better than no location
195
+ return true ;
196
+ }
197
+
198
+ // Check whether the new location fix is newer or older
199
+ long timeDelta = newLocation .getTime () - currentBestLocation .getTime ();
200
+ boolean isSignificantlyNewer = timeDelta > TWO_MINUTES ;
201
+ boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES ;
202
+ boolean isNewer = timeDelta > 0 ;
203
+
204
+ // If it's been more than two minutes since the current location, use the new location
205
+ // because the user has likely moved
206
+ if (isSignificantlyNewer ) {
207
+ return true ;
208
+ // If the new location is more than two minutes older, it must be worse
209
+ } else if (isSignificantlyOlder ) {
210
+ return false ;
211
+ }
212
+
213
+ // Check whether the new location fix is more or less accurate
214
+ int accuracyDelta = (int ) (newLocation .getAccuracy () - currentBestLocation .getAccuracy ());
215
+ boolean isLessAccurate = accuracyDelta > 0 ;
216
+ boolean isMoreAccurate = accuracyDelta < 0 ;
217
+ boolean isSignificantlyLessAccurate = accuracyDelta > 200 ;
218
+
219
+ // Check if the old and new location are from the same provider
220
+ boolean isFromSameProvider = isSameProvider (newLocation .getProvider (),
221
+ currentBestLocation .getProvider ());
222
+
223
+ // Determine location quality using a combination of timeliness and accuracy
224
+ if (isMoreAccurate ) {
225
+ return true ;
226
+ } else if (isNewer && !isLessAccurate ) {
227
+ return true ;
228
+ } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider ) {
229
+ return true ;
230
+ }
231
+ return false ;
232
+ }
233
+
234
+ /** Checks whether two providers are the same */
235
+ static boolean isSameProvider (String provider1 , String provider2 ) {
236
+ if (provider1 == null ) {
237
+ return provider2 == null ;
238
+ }
239
+ return provider1 .equals (provider2 );
240
+ }
186
241
187
242
private static class MyLocationListener
188
243
implements LocationListener {
@@ -263,4 +318,4 @@ public interface OnLocationChangeListener {
263
318
*/
264
319
void onStatusChanged (String provider , int status , Bundle extras );//位置状态发生改变
265
320
}
266
- }
321
+ }
0 commit comments