Location Services in Android Development - sahajss/knowledge_base GitHub Wiki

#Location Services in Android Development

###tldr Use Google's FusedLocationProviderApi instead of the built-in Android location listener with GPS/Network provider. If that's all you want from this, thank you for reading this far. If you are interested in the reasons behind my assertion, I invite you to continue below.

###What Does Android Provide? Android gives you access to the location system service and, from which, you have a few options for retrieving location data. The requestLocationUpdates() function has multiple sets of arguments it can take, but the two consistent values are minDist and minTime. Their values indicate the minimum change in distance and minimum change in time before another location update can be sent.

The basic set up for requesting location updates requires a location listener. The LocationListener gives you four callback functions: onLocationChanged(), onStatusChanged(), onProviderEnabled(), and onProviderDisabled(), which give you access to the location of the phone and the status of the GPS provider.

Sounds good so far? Here's the negative. The GPS and network providers by themselves have inconsistent, slow, and inaccurate readings. The accuracy of a location -- given by a radius from which there is a 68% confidence the true location lies within that radius -- can regularly vary between 200 and 1200 meters. Having a location be over a kilometer from the true location is a serious issue. Not only that, the updates at inconsistent intervals, sometimes taking towards a minute before triggering a callback.

If you do not need your location every few seconds or do not need it accurate to under 100 meters, then perhaps you could use the Android-provided LocationManager and LocationListener. In that case, you can probably get away with getting your location data passively from other apps. However, should you need the speed and accuracy as you might expect in Google Maps or Waze, you will want to use Google's FusedLocationProviderApi.

###Why use the FusedLocationProviderApi? As stated, the FusedLocationProviderApi provides you with a quick and reliable method of grabbing location data. You have the ability to control the interval at which you get updates (though be careful to conserve battery power) and the location will actually be updated within that time frame. On top of this, the accuracy consistently stays around a 20-meter radius, a dramatic improvement over 1200 meters. The accuracy is extremely reliable, with rare spikes up to 50-meter radii.

The backend to the FusedLocationProviderApi is much more complicated that I can explain in brief. In essence, it provides a more stable and reliable access to location data through the combination of location providers.

###How to use FusedLocationProviderApi? Well, I could detail how to use it, but why repeat what's already out there? There are tutorials on Android Developer, TeamTreeHouse, and Java Papers. TeamTreeHouse has consistently top quality tutorials and videos (though the majority of which are not free without subscription). I personally liked the Java Papers despite it being more of an example than a tutorial.

If you made it this far, I greet you with the cliche "if you made it this far" sentence.

David Zhao '16