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

10 comments:

  1. Its a good tutorial covering all stuff to get current location. Can you post a tutorial for showing you current location on google maps too?? That'd be great!
    Thanks!

    ReplyDelete
    Replies
    1. Thanks for appreciating the effort!
      Sure I'll post a tutorial about google maps activity shortly.

      Delete
  2. Replies
    1. Can you please share the error? Meanwhile check if you have added permissions in androidmenifest.xml

      Delete
    2. In the emulator and on a Galaxy S7 the LAT/LONG are never displayed. The working dial spins but never stops. If I turn off location the App asks to turn it on and that happens.

      Any suggestions about how to correct this issue?

      Thank you,

      Richard

      Delete
  3. Working on Lollipop, but not on Marshmallow and Nougat. Tried to add in code permission - didn't help. Any suggestion what could go wrong. Thanks.

    ReplyDelete
  4. compile 'com.google.android.gms: play-services: 10.0.1'
    kenapa kok setiap kali gradle eror?? butuh pencerahan

    ReplyDelete
  5. it doesnt show location with gps only?

    ReplyDelete
  6. How can we get the distance between two locations using this same example?

    ReplyDelete
  7. Is it possible to get the location name as well with lat and long?

    ReplyDelete