Showing posts with label google maps. Show all posts
Showing posts with label google maps. Show all posts

Sunday, 15 January 2017

Android tutorial to get last known location using Google Fused Api

androdi-tech-point
This tutorial is about getting last known location using Google Fused Api is similar to getting the current geolocation of the device. But the advantage of using the last known location the if some data about location is already available, it does not request for the current geolocation. Which save the battery consumption and data usage of the device which is always a big problem for the developers.

Here is the demo video of the running app.

In this tutorial, we will do the following tasks:
  • Get the last known location using google fused api.
  • If no data is available, we'll request for the current geolocation updates.
  • Add check for enabling location services of the device.

But first things first. Set up environment for using google play services. For this you need to download the 'Google Play Services' form SDK Manager.

After that add the dependency in the build.gradle(App Module) file:
compile 'com.google.android.gms:play-services:10.0.1'

set up complete for using the getting current location using google fused api.
Add permissions in AndoidManifest,xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

You can get the complete project at Github Repository. Or you can get the code below in this post.
All done, just add the following code in your MainActivity.java

And the following code in your main_activity.xml