An android app that uses Google Maps API and SDK to track a user's location and calculate the total distance travelled

Overview

Bike Rush

Bike Rush is an android app that uses Google Maps API and SDK to track a user's location and calculate the total distance travelled by him or her along with time and average speed.

The app will keep tracking the user even when it is closed or screen is turned off. Additionally, the app also shows an interactive Distance-Time graph of a user's journey.

Note:- To test the app, I have used Fake GPS Location Spoofer to quickly change my location from one place to another. This is the reason why speed is shown too high in the above app screenshots.

Project Purpose

The purpose of this project is to demonstrate how can a person:

  • Use Google Maps API and Android Location Services to get the current location of user and draw it on the map.
  • Calculate the total distance, time and average speed of the user in his or her overall journey and store it in database.
  • Start a foreground service and update the notification every second to notify the user about total journey tracking time elapsed.
  • Use Interactive Line Charts to display the statistics of total number of journeys made by the user.

Below I have explained the flow of app. Moreover, I have also commented what I am doing in a particular function or class in the app's source code so that one can easily understand it. In any case if you do not understand something, feel free to contact me.

Project Setup

Adding Dependencies

Before we get started, we need to add the following dependencies to your build.gradle app

  • Google Maps Location Services - This dependency will let us use Google Maps SDK in our app using our API Key.
  • Room Persistance Library - This library provides an abstraction over SQLite and makes it a lot simpler to save and fetch data from a device's database.
  • Coroutines - Coroutines are used to perform two or more tasks parallely or asynchronously. They are very useful when you need to load data from cloud into your app.
  • Dagger Hilt - Dagger Hilt is a library that provides an abstraction over Dagger. We will use this library to implement Dependency Injection in our app.
  • Glide - Glide is an image loading library. It makes it really easy to load images from cloud into our app. Moreover, we can also set placeholder and error images and also change the quality of images.
  • Easy Permissions - This dependency lets us easily check whether the user has provided access to a certain permission or not and also lets us manage when to show permission rationale and open android permission settings.
  • Architectural Components - To use Viewmodel in our app, we will use this library.
  • Navigation Component - This is the Android's library that makes it really easy to navigate from one fragment to another.
  • Google Material Design - This library provides a huge collection of ready to use beautiful UI components that are much better than Android's default UI components.
  • Timber - Timber is a library that helps you in debugging your code by logging messages at different parts of your code. It is much more efficient than the default Log() method.

App Architecture

drawing

I have used MVVM architecture in this app. The flow of the app is as follows:

  1. Journey.kt
    This class defines data about a single journey that user will make including details such as distance travelled, duration of journey, average speed, date created and image of journey.

  2. JournetDAO.kt
    This class defines the operations we will perform using Journey.kt class such as insertion, deletion and fetching of data from database.

  3. Journey Database
    This class creates the database of our journey into the device.

  4. Bike Rush Repository
    Purpose of a repository is to collect data from database and any cloud storage. In BikeRushRepository.kt, we will simply take the data from JourneyDatabase.kt.

  5. Bike Rush Viewmodel
    This class will launch the coroutines and call the functions inside BikeRushRepository.kt.

  6. Bike Rush Activity
    This is a simple activity to hold all of our fragments.

  7. Journey Fragment
    This fragment will display list of all of our journeys in the android database.

  8. Statistics Fragment
    This fragment will display the interactive line chart using all the journey related data present inside android database.

  9. Tracking Activity
    This class will display the map, draw the track of user on it and also display the realtime values of speed, distance and time.

  10. Tracking Service
    This service will make the actual requests for the user's location, make the calculations and monitor the changes of speed, distance, time and also display the notification.

Helper Classes

These are some of the classes I created to optimize my code a little more and perform some more tasks:

  1. TrackingUtility.kt
    This class contains the functions to check the location permission, convert total number of seconds to hh:mm:ss string format and calculate the total length of path travelled by the user.

Dagger Hilt related files

  1. AppModule.kt
    This file contains the objects we only need as long as our application lives.

  2. ServiceModule.kt
    This file contains the objects we only need as long as our service lives.

  3. BikeRushApplication.kt
    This file provides Application Context and Timber Debug Instance to all our functions.

Other important files

  1. Constants.kt
    This file contains all the constant variables we require in our app such as Notification IDs, Path color and thickness, Google map zoom, etc.

  2. Converters.kt
    Android Room database cannot store image as bitmap so in this class, we have converted image to byte array to be stored in database and also convert byte array back to a bitmap to be displayed in the image view.

  3. JourneyAdapter.kt
    This class defines how many journey items are to be displayed in JourneyFragment.kt, how would they look like and how to assign data such as image and text values from the android database to those journey layout items.

Google Maps API setup

This is really simple. All you need to do is:

  1. Go to Google Cloud Platform and create an account.
  2. Create a project for Android SDK and get your Google maps key.
  3. Then in strings.xml, paste the API key in the following format:

Paste your API Key here

Once you do this, you are all set to use google maps in your app :)

Note:- For security purposes, I haven't added strings.xml file in this github repository. But you can easily clone this project, go to res folder, and inside it, go to values and create strings.xml and paste your API key there.

Contact

For any queries, you can mail me at [email protected]

You might also like...
Curve-Fit is an Android library for drawing curves on Google Maps
Curve-Fit is an Android library for drawing curves on Google Maps

Curve-Fit Android library for drawing curves on Google Maps. This library uses Bezier cubic equation in order to compute all intermediate points of a

Demo de uso de google maps en Android, charla para el GDG Chimbote

mapasbasico Demo de uso de google maps en Android, charla para el GDG Chimbote Puedes usar este proyecto como base para trabajar con mapas en Android.

Membuat Custom Tooltip Marker Google Maps
Membuat Custom Tooltip Marker Google Maps

Custom-Tooltip-Marker Membuat Custom Tooltip Marker Google Maps Tutorial Build with Android Studio https://youtu.be/E8ND0YThNiU Tutorial Build with St

This project allows you to  calculate the route between two locations and displays it on a map.
This project allows you to calculate the route between two locations and displays it on a map.

Google-Directions-Android This project allows you to calculate the direction between two locations and display the route on a Google Map using the Goo

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.
Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.

TileOverlay component for react-native-maps

TileOverlay component for react-native-maps

Positional is a location information app for Android with Compass, Clock, Level, Sun and Moon and many other features.
Positional is a location information app for Android with Compass, Clock, Level, Sun and Moon and many other features.

Positional is a location based app that utilizes the device's GPS and fetches various details of the current latitude and longitude data like Altitude, Speed, Address and similar other information and show it in easily understandable format to the user. Alongside this main functionality of being a location app, Positional also provides a separate panel for Compass and Clock, and they serve their own purpose as their name suggests.

Android library project that lets you manage the location updates to be as painless as possible

Smart Location Library Android library project that intends to simplify the usage of location providers and activity recognition with a nice fluid API

Android library that allows you to determine your location in a few of lines
Android library that allows you to determine your location in a few of lines

locsimple Android library that allows you to determine your location in some lines! Benefits: automatic processing of permissions processing of enabli

Owner
Ishant Chauhan
I love solving real world problems using my programming skills!
Ishant Chauhan
Android Maps Extensions is a library extending capabilities of Google Maps Android API v2.

Android Maps Extensions Library extending capabilities of Google Maps Android API v2. While Google Maps Android API v2 is a huge leap forward comapare

Maciej Górski 408 Dec 15, 2022
Maps application in Android Studio using the Maps SDK for android

Google-Maps-Application Maps application in Android Studio using the Maps SDK for android. This app was initially developed in Java, but then was conv

Kyle McInnis 0 Nov 30, 2021
Scale bar for Android Maps (Google Maps, OSM, MapBox, Yandex)

Map Scale View Scale view for any Android Maps SDK (not only Google Maps) Contributing I encourage you to participate in this project. Feel free to op

Stas Parshin 109 Nov 18, 2022
malik dawar 87 Sep 18, 2022
🍃 Organic Maps is an Android & iOS offline maps app for travelers, tourists, hikers, and cyclists based on top of crowd-sourced OpenStreetMap data and curated with love by MapsWithMe founders.

?? Organic Maps is an Android & iOS offline maps app for travelers, tourists, hikers, and cyclists based on top of crowd-sourced OpenStreetMap data and curated with love by MapsWithMe founders. No ads, no tracking, no data collection, no crapware.

Organic Maps 4.3k Dec 31, 2022
App usage tracker which maps your app usage to geo location.

Guilt Guilt is an inspiration from Meta (pun intended), it tracks the apps usage and maps it with geo location data where the app was last used. The a

null 6 Dec 28, 2022
Google Maps Api test using marker rotation and routes.

Google Maps Api test using marker rotation and routes. Features ✔️ Kotlin ✔️ DI: Hilt ✔️ Retrofit ✔️ Gson ✔️ View binding ✔️ Coroutines ✔️ AndroidX ✔️

Carlos Adan 39 Jul 15, 2022
EasyRoutes allows you to easily draw routes through the google maps address api.

EasyRoutes EasyRoutes allows you to easily draw routes through the google maps address api. Note: You need to generate an API key from the google cons

Antonio Huerta Reyes 7 Jul 26, 2022
Maps SDK for Android Utility Library

Maps SDK for Android Utility Library Description This open-source library contains utilities that are useful for a wide range of applications using th

Google Maps 3.4k Dec 30, 2022
An app to search nearby businesses on Google Maps & Add Grocery Items to List!

GoStore: Internship Program Project A mobile app is built where the user can search for his nearby locations based on his requirement. Whenever the us

null 7 Nov 28, 2022