logo

Android - Location

Last Updated: 2021-11-19

Regardless of which API to call, here are the ways to get the location of a device:

  • GNSS: Global Navigation Satellite Systems, including GPS, GLONASS, BeiDou, etc.
  • aGPS: assisted GPS, GPS data collected by cellular towers
  • Network: by Wifi and Cellular data
  • AR

AOSP: Location and Location Manager

Android provides Location and LocationManager for device's locations:

  • android.location.Location: a data class
  • android.location.LocationManager: generates Locations

Location class has a method getProvider() that may tell you which provider is used to generate this location.

  • GPS_PROVIDER: (GPS, aGPS), accurate, high power usage, only works outdoors
  • NETWORK_PROVIDER: (aGPS, CellID, Wifi MACID): relies on availability of cell towers and Wifi access points, lower accuracy and lower power usage.

Note that Location's getProvider() only returns a string, while LocationManager's getProvider() returns the full LocationProvider object with more info.

This is NOT the officially recommended way to get locations on Android, the recommended way is to use Google Play Services as described in the next section. However not all Android devices support Google Play Services.

Google Play Services: FusedLocationProvider

FusedLocationProvider will not only use GPS, but also Wifi and Cellular data, and "fuse" everything into one best estimated location. This is only available through Google Play Services, not included in the open-source Android.

GPS has small probability of large outliers, but the signal may be lost if it cannot acquire enough satellites; Cell and wifi are land based, but locations have all sorts of reasons for having large outliers (even the wrong continent). That is why fused locations may provide better results comparing to using them alone.

The API is com.google.android.gms.location.FusedLocationProviderClient, which extends GoogleApi.

Ref: https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient

https://developer.android.com/reference/android/location/Location

Not Public: RoadSnapped Location

If you are navigating, your location is snapped to the nearest road, however the logic is not public and the road-snapped location is not public, but be aware that this might be different from the raw Location or FusedLocation.

Location Spoofing

In some cases users may choose to spoof their locations, e.g. when you play Pokemon Go, or a rideshare driver pretends he is waiting at the airport.

There are many fake GPS apps, most requires to enable the developer options to allow mock locations. Detecting GPS spoofing is a whole different topic.