Skip to content

Commit 14c83cc

Browse files
authored
Merge pull request Blankj#178 from ahmadmust8/patch-2
update LocationUtils.java
2 parents e408532 + 193a307 commit 14c83cc

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

utilcode/src/main/java/com/blankj/utilcode/util/LocationUtils.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public final class LocationUtils {
2929
private static OnLocationChangeListener mListener;
3030
private static MyLocationListener myLocationListener;
3131
private static LocationManager mLocationManager;
32+
private static final int TWO_MINUTES = 1000 * 60 * 2;
3233

3334
private LocationUtils() {
3435
throw new UnsupportedOperationException("u can't instantiate me...");
@@ -183,6 +184,60 @@ public static String getStreet(double latitude, double longitude) {
183184
Address address = getAddress(latitude, longitude);
184185
return address == null ? "unknown" : address.getAddressLine(0);
185186
}
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+
}
186241

187242
private static class MyLocationListener
188243
implements LocationListener {
@@ -263,4 +318,4 @@ public interface OnLocationChangeListener {
263318
*/
264319
void onStatusChanged(String provider, int status, Bundle extras);//位置状态发生改变
265320
}
266-
}
321+
}

0 commit comments

Comments
 (0)