Giter Club home page Giter Club logo

android-location-tracker's Introduction

android-location-tracker

Android Simple Location Tracker is an Android library that helps you get user location with a object named LocationTracker

Android Arsenal

Installation

Add this to your build.gradle file

repositories {
    maven {
        url "https://jitpack.io"
    }
}

dependencies {
        compile 'com.github.quentin7b:android-location-tracker:4.0'
}

Don't forget to add the following permissions to your AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Be aware of Android Marshmallow's new permission system

<permission-group
   android:name="android.permission-group.LOCATION"
   android:label="A label for your permission"
   android:description="A description for the permission" />

Use

As its name says, it's a simple library. To create a tracker you just need to add the below code in your Android Activity / Service / WorkManager

Be aware, you'll have to manage runtime permissions on Manifest.permission.ACCESS_FINE_LOCATION & Manifest.permission.ACCESS_COARSE_LOCATION

Create the tracker

Constructor is defined as this

val locationTracker = LocationTracker(
    val minTimeBetweenUpdates: Long = 5 * 60 * 1000.toLong(),
    val minDistanceBetweenUpdates: Float = 100f,
    val shouldUseGPS: Boolean = true,
    val shouldUseNetwork: Boolean = true,
    val shouldUsePassive: Boolean = true
)

Add a listener

locationTracker.addListener(object: Listener {

	fun onLocationFound(location: Location) {
	}

	fun onProviderError(providerError: ProviderError) {
	}

});

Start and stop listening

locationTracker.startListening(context)
//and
locationTracker.stopListening()

Provide custom use

You can create a LocationTracker with custom parameters.

  • minTimeBetweenUpdates minimum time between two locations to respect before notifying the listeners in milliseconds). Default is 5 minutes
  • minDistanceBetweenUpdates minimum distance between two locations to respect before notifying the listeners in meters). Default is 100 meters
  • shouldUseGPS specifies if the tracker should use the GPS locations or not. Default is true
  • shouldUseNetwork specifies if the tracker should use the GPS locations or not. Default is true
  • shouldUsePassive specifies if the tracker should use the passive locations or not. Default is true

With the default parameters, when a location is found, the tracker will not call onLocationFound() again during 5 minutes. Moreover, if the distance between the new location and the older one is less than 100m, onLocationFound() will not be called.

Be aware that the priority of the time parameter is higher than the priority of the distance parameter. So even if the user has moved from 200km, the tracker will call onLocationFound() only after 30 minutes.

Cleaning

Be aware! A LocationTracker never stops running until you tell it to do so.

If the tracker is running in foreground and not in a service, it might be a good idea to link to the lifecycle

The stopListening method has an optional parameter cleanListeners that is false by default. (calling stopListening(false) is the same as calling stopListening()).

When calling stopListening we do not remove the listeners you've set, so they will be notified once you start listening again. But calling stopListening(true) will clear the list of registered listeners (same as calling removeListener for every registered listener).

tracker.addListener(listener)
tracker.startListening(context)
// listener will be notified
...
tracker.stopListening()
// listener won't receive updated
tracker.startListening(context)
// listener will be notified
...
tracker.stopListening(cleanListeners = true)
// listener won't receive updated for ever
tracker.startListening(context)
// listener won't be notified

android-location-tracker's People

Contributors

akhy avatar mroutis avatar quentin7b avatar stephenchips avatar yasiralijaved avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.