Now you can Subscribe using RSS

Submit your Email

Wednesday

How to create a Location Tracker App in Android complete tutorial

Vishal Shrestha
Creating a basic but functional Location Tracker app in Android is not much work, you can do it within an hour! Just follow the following tutorial. Here we will create an Android App that will access GPS or Network to figure out your current location and keep updating the Marker on the map to give the effect of "Tracking". We will be using Google Maps Android API, Android Studio and an Android Phone to test.

If you are into videos : You can watch this YouTube Video : Create an Android Location Tracker app in 30 Minutes.

Note - Find Source code of this location tracker app at the end of this article

Let's begin to create our very own Location Tracker App! Create a new project and Select the Google Maps Activity as shown in the figure below and just click next on the corresponding options and click on finish. Doing this gives us a template that already has a layout with maps fragment and an associated activity with the marker placed somewhere over Australia! We can run this app now by just adding our API_KEY.

Creating_location_tracker_app_android
Creating new MapsActivity Project


You will see a screen like this, at the second last line, notice YOUR_KEY_HERE. You need to get the API key for using google maps API for Android. You will need Google Maps to display your location on the map.

Google maps android API key
API_KEY


To get the API Key, go to this link - Google Maps Android API, and a webpage like the one below will open up. Now just click on the Maps link(The first option form the bottom image row). Update - Now you can click on GET A KEY button on top right, next to pricing and plans button.If you do this, skip the next step.

getting google maps android api key
Official Google Maps website



Now if you can see a page like this, click on the GET A KEY button right next to View Pricing and Plans button on the top right corner of the page.

Getting google maps android api key
Getting google maps API Key


Now you will be show a dialog box like the one shown below, here you need to give the project name for which you will be using Google Maps.

Google maps app android project name
Google Maps App Project Name


After you enter a valid project name, you will be shown a new dialog box with the you API KEY, Bravo. Now you can finally start coding you own Location Tracker app for Android. Copy the API_KEY shown to you in the dialog box, you will need it in the next step.

Google Maps Android API Key
Google Maps Android API Key


Now we are back in Android Studio, let's begin where we left off. Paste the API KEY that you copied from the dialog box in the previous step in the highlighted part shown in the screenshot below.
At this point we already have a functional app that puts the Marker over some place in Australia. The marker is static and you can change it to be placed over any place by defining the coordinates in the MapsActivity. You can run and test this app now, just add the following permissions in the Manifest :
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.INTERNET"/>

Android Studio Google Maps API Key
Android Studio Google Maps API Key



Now open your activity_maps.xml from the layouts folder inside res folder. It should look like the image shown below.

Google Maps Android Studio MapsActivity
MapsActivity


Now finally we can start some actual coding! Read, keep or remove the pre defined comments, I choose to remove it. Now create a new GoogleMap variable mMap and implement LocationListener. You need the LocationListener to be notified whenever your location changes. The implementation will show some error for now, it's because we haven't implemented the required methods yet.

LocatioinListener for getting location changes
Location Listener


To remove that error, implement all the required methods. Click ALT + Enter and you will be shown list of all the methods that can be implemented. Select all of them and click Enter. You will see the following methods and the error will have been resolved. Although there are a bunch of methods, we will only be using onLocationChanged which is called whenever new location data is received by the device. Here we handle what to do with the new data, put the marker over that point or do whatever you want with it.

Implementing LocationListener Methods
Implementing location listener methods


Now, create your own method called requestLocation. This method will be responsible for setting the criteria of the location requests that you will make. Criteria defines the accuracy level required and the power level required. According to your criteria the Android system automatically chooses GPS or Network for getting the your location so that you can focus on what you want rather that choosing the tool that you will be using.

Requesting location update from LocationManager
LocationManager and Criteria


Now you need to call the requestLocation method so the the device can try to figure out your location. But first you need to do some preparations, if you want your app to run on devices running Marshmallow or higher, you need to take care of Run Time permissions. To learn more about run time permissions, read this post - Marshmallow Runtime Permissions. You also need to add a new global Marker and MarkerOptions variable as you want to update the position of the same marker not create a new marker every time your position is changing. Initially we've set the marker position to 0,0. You'll see a lot of error for now as all the methods we've call are not yet defined. They are defined at the end of this tutorial. Write the code shown below in the screen shot.

Requesting location access permissions
Location Service Permissions


Since you have now called the requestLocation methods which requests your current location from the LocationManager, you can get you location data any second, so you need to handle your location data in the onLocationChanged method. Add the code shown below in onLocationChanged and add the marker to that place in the onMapReady.

Using onLocationChanged Method and using marker
Adding Marker on Map
Here's the code for the isLocationEnabled and isPermissionGranted method. isLocationEnabled checks if the location information can be given to apps i.e if apps are allowed to get the location information or not. isPermissionGranted method checks specifically for your app if your app is allowed to access location services or not. If not allowed it requests the permissions to the user.

Checking if location service is enabled
Checking if location service is enabled


Finally here's the code of showAlert. It shows a dialog box if the location services in your device is turned off, i.e if apps are not allowed to access you current location data. If the user choose to give the permissions then he/she is directly taken to the location setting options.

Showing alert for location service request
Showing Dialog Box

Finally our app is done! it wasn't that complex, was it? We have our own Location Tracker app which updates the marker in the map according to our position. The marker changes the position abruptly but the app tracks the location just fine. We can work on the smoothness of the change of marker's location some other time. Happy coding, if you have any problems, don't hesitate to comment below.

Source Code : Download Project Files
Note : Please bear with me and complete the survey presented to unlock the download, but for some reason if you cannot download then please feel free to comment below or mail me, I'll get back to you as soon as possible.

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

4 comments:

  1. Replies
    1. DSownload Link : http://www.filefactory.com/file/60ov2agxtdxt/LocationTracker.rar

      Delete
  2. same here im also not able to download,
    plz provide me the download link...

    ReplyDelete
    Replies
    1. DSownload Link : http://www.filefactory.com/file/60ov2agxtdxt/LocationTracker.rar

      Delete

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha