Git Product home page Git Product 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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-location-tracker's Issues

Error on adding library by Gradle

When add android-location-tracker to my dependencies I get an error:
Error:Failed to resolve: com.android.support:support-annotations:23.1.1

why is that?

Callback for "not enabled" errors

Provide a callback for this problems:

  • GPS_PROVIDER is not enabled
  • NETWORK_PROVIDER is not enabled
  • PASSIVE_PROVIDER is not enabled

leak issue found

There was an anonymous subclass of fr.quentinklein.slt.LocationTracker and the anonymous implementation of java.lang.Runnnable.
This issue was tracked by leakcanary

Almost duplicate requests to onLocationFound

Hi - great library, thanks for developing and sharing.

I have an issue in that I seem to be getting duplicate calls to onLocationFound(Location loc). They are not exact duplicates in that they are usually within ~100 milliseconds of each other. Below is output from using 30 second time interval using your example code:

settings = new TrackerSettings()
.setUseGPS(true)
.setUsePassive(true)
.setUseNetwork(true)
.setTimeBetweenUpdates(1000 * 30)
.setMetersBetweenUpdates(20);

The output is Latitude, Longitude, Accuracy, Time (millisecs), Cumulative distance. This was collected when I was walking tracking a route.

52.3980456,-1.526949, 18.955, 1477316971912, 6
52.3980456,-1.526949, 18.955, 1477316971974, 6

52.39800801,-1.52702431, 9.0, 1477316973661, 13
52.39777663,-1.52684439, 5.0, 1477316992652, 42
52.39777663,-1.52684439, 5.0, 1477316992681, 42

52.3975514,-1.52639864, 4.0, 1477317013671, 81
52.39745864,-1.5262795, 8.0, 1477317023666, 94
52.3977192,-1.5264435, 23.388, 1477317033140, 125
52.39740684,-1.52612629, 5.0, 1477317033711, 166
52.39738598,-1.52595222, 9.0, 1477317042658, 178
52.3973101,-1.52579648, 7.0, 1477317052669, 192
52.3975554,-1.5262395, 19.427, 1477317053187, 232
52.3975554,-1.5262395, 19.427, 1477317053210, 232
52.39716856,-1.5253993, 8.0, 1477317073681, 304
52.39716856,-1.5253993, 8.0, 1477317073714, 304
52.396897,-1.52528835, 6.0, 1477317093664, 335
52.396897,-1.52528835, 6.0, 1477317093677, 335
52.39666517,-1.52568172, 6.0, 1477317113081, 372
52.39666517,-1.52568172, 6.0, 1477317113094, 372
52.39656626,-1.5259295, 5.0, 1477317123083, 392
52.39656626,-1.5259295, 5.0, 1477317123093, 392

52.39666626,-1.52624856, 8.0, 1477317144116, 417
52.39671506,-1.52649306, 5.0, 1477317153693, 434
52.39678975,-1.52667512, 6.0, 1477317164102, 449
52.3968655,-1.52684107, 4.0, 1477317173730, 463
52.39697536,-1.52708833, 8.0, 1477317183689, 484
52.39697536,-1.52708833, 8.0, 1477317183705, 484

52.39714838,-1.52749132, 8.0, 1477317204116, 518
52.397255,-1.52766193, 6.0, 1477317213695, 534
52.39750574,-1.52741519, 9.0, 1477317223083, 567
52.39751272,-1.52729175, 8.0, 1477317233115, 575
52.39755086,-1.527116, 6.0, 1477317244102, 588
52.39767366,-1.52696935, 9.0, 1477317254678, 605
52.39778008,-1.52685321, 6.0, 1477317264115, 619
52.39791,-1.52683434, 8.0, 1477317273103, 634
52.39803259,-1.52688624, 6.0, 1477317284112, 648
52.39813587,-1.52689322, 9.0, 1477317293677, 659

I can easily ignore these almost duplicate values (in bold), but wondered if you can explain why they might be happening?
Many thanks - and again, useful and easy to use library!

onTimeout() not working

I'm using the code below in a Service. Everything works, except for the onTimeOut();

How to reproduce:

  1. Turn on Location on device
  2. Load an app with the code below
  3. Once it starts the tracking, don't move the phone
  4. Wait for the 10 seconds and see how there's no logged message
        TrackerSettings settings =
                new TrackerSettings()
                        .setUseGPS(true)
                        .setUseNetwork(true)
                        .setUsePassive(true)
                        .setTimeBetweenUpdates(10 * 1000); // 10 sec

        LocationTracker tracker = new LocationTracker(getApplicationContext(), settings) {
            @Override
            public void onTimeout() {
                Log.d("LocationTracker", "onTimeout");
            }
            @Override
            public void onLocationFound(Location location) {
                Log.d("LocationTracker", "onLocationFound");
            }
        };
        tracker.startListening();

gradle sync fail

implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
implementation 'com.google.firebase:firebase-auth:11.8.0'
implementation 'com.google.firebase:firebase-database:11.8.0'
implementation 'com.google.firebase:firebase-storage:11.8.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.master.android:permissionhelper:1.3'
compile 'com.android.support:design:27.0.2'
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'

here my other dependecy....is this support app compect activity?

Enhanced LocationTracker.Listener

In the latest version (you rewrote the library in Kotlin, version 4.0), please add onProviderDisabled, onProviderEnabled, onStatusChanged methods to the LocationTracker.Listener interface.

With the previous version, written in Java, I used to extend the LocationTracker class. This class was implementing android.location.LocationListener so I used to override onProviderDisabled, onProviderEnabled in the extended class. Now I cannot do this anymore.

Background Tracking

Hello!

Did this library do on background tracking? (App not close, just put on background). Because, when I use SetGPS(true) (Network and Passive are false) the location little bit jump between point.

Thank you!

Waze problem

The app works fine but when I open Waze, does not track. Do you know any reason? Thanks in advance

GPS distance calculation error in android-location-tracker

My mobile was kept on my desk for almost 12hrs and finally it says total distance travelled is 54 km.
For lat long capturing I have my parameters as:
Time Interval: 60 seconds
Distance: 100 metres
Location Provider: GPS

For distance calculation in android I'm using Haversine Formula.

any suggestion to improve the distance calculation.

TimeOut Issue

My app is continuously (5hrs continuously while Network & GPS is available) getting onTimeOut() for location TrackerSettings. I tried for to STOP (tracker.stopListening()) then START (tracker.startListening()) but it's not working. still getting same onTimeOut(). Please help me out fot this.

My Code :-

private final long LOCATION_TRACKING_INTERVAL = 1000 * 60 * 5; 
private final float TRACKING_DISTANCE = 1.0f;
LocationTracker tracker = new LocationTracker(getApplicationContext(), new TrackerSettings()
                .setUseGPS(true)
                .setUseNetwork(true)
                .setUsePassive(false)
                .setMetersBetweenUpdates(TRACKING_DISTANCE)
                .setTimeBetweenUpdates(LOCATION_TRACKING_INTERVAL)) {
           
 @Override
            public void onLocationFound(@NonNull Location location) {
                writeToSDFile("onLocationFound");
                writeToSDFile("Location Latitude : " + location.getLatitude() + " Longitude : " + location.getLongitude() + " Provider : " + location.getProvider());
            }
           
 @Override
            public void onTimeout() {
                try {
                    writeToSDFile("onTimeout");
                     if (tracker != null && tracker.isListening()) {
                             tracker.stopListening();
                             tracker.startListening();
                    }
            }
        };
        tracker.startListening();

onLocationFound

onLocationFound() can't update???only get one location。。。

Getting 'Provider is not enabled | ProviderError{provider='network'}' on calling 'tracker.startListening();'.

I'm using your library in my app and while trying this code:


settings =
                new TrackerSettings()
                        .setUseGPS(true)
                        .setUseNetwork(true)
                        .setUsePassive(true)
                        .setTimeBetweenUpdates(30 * 60 * 1000);

        tracker = new LocationTracker(getBaseContext(), settings) {
            @Override
            public void onLocationFound(Location location) {
                // Do some stuff
                currentLatDouble = location.getLatitude();
                currentLngDouble = location.getLongitude();
            }

            @Override
            public void onTimeout() {

            }
        };
        tracker.startListening();

I'm getting these errors:

W/LocationTracker: Provider (gps)
                   fr.quentinklein.slt.ProviderError: Provider is not enabled | ProviderError{provider='gps'}

W/LocationTracker: Provider (network)
                   fr.quentinklein.slt.ProviderError: Provider is not enabled | ProviderError{provider='network'}

even when the 'WiFi' is enabled!

Please help with this issue!

Not working in WorkManager

I am using this code:

private void getLocationUpdates() {

            TrackerSettings settings =
                    new TrackerSettings()
                            .setUseGPS(true)
                            .setUseNetwork(true)
                            .setUsePassive(true);
//                            .setTimeBetweenUpdates(5000);
//                            .setTimeBetweenUpdates(30 * 60 * 1000)
//                            .setMetersBetweenUpdates(100);

            LocationTracker tracker = new LocationTracker(getApplicationContext(), settings) {
                @Override
                public void onLocationFound(Location location) {
                    // Do some stuff

                    String latlng = location.getLatitude() + ", " + location.getLongitude();

                    Log.d("WorkManager", "onLocationFound: " + latlng);

                }

                @Override
                public void onTimeout() {
                    Log.d("WorkManager", "onTimeout: ");
                }
            };
            tracker.startListening();
    }

in the doWrok() function of WorkManager.
But this code works in the Service.

Error while using library in background service

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    at android.os.Handler.<init>(Handler.java:200)
    at android.os.Handler.<init>(Handler.java:114)
    at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:223)
    at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:223)
    at android.location.LocationManager.wrapListener(LocationManager.java:851)
    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:864)
    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:459)
    at fr.quentinklein.slt.LocationTracker.startListening(LocationTracker.java:111)
    at com.mypackage.api.gcm.GcmMessageListener.sendLocation(GcmMessageListener.java:85)

Possible setTimeBetweenUpdates() issue

Hi - just wanted to raise an issue I've recently encountered (possibly anecdotal and not tested thoroughly), but couldn't find any documentation defining or discussing it.

I had .setTimeBetweenUpdates(500) set, and some devices were reporting very slow location updates, up to about 9-10 seconds when it should have been every 1/2 second.

I slowed location polling down to .setTimeBetweenUpdates(1000) and it started updating at the correct times on all affected devices.

I wondered if there was a minimum value that should be mentioned in the docs, or a bug in the method? Either way it's fixed my issues, might help others.

onLocationFound called twice

Hi there!

my settings are

LocationTracker(60000L, 0f, shouldUseGPS = true, shouldUseNetwork = true, shouldUsePassive = true)

when location is found, onLocationFound is called twice.

any solution to this?

location updates delay issue

It updates the location of the device very slow (more than 5 mins to update).

This is the setting of the location tracker:

    TrackerSettings settings =
            new TrackerSettings()
                    .setUseGPS(true)
                    .setUseNetwork(true)
                    .setUsePassive(true)
                    .setTimeBetweenUpdates((1 / 2) * 60 * 1000)
                    .setMetersBetweenUpdates(10);

    LocationTracker tracker = new LocationTracker(getContext(), settings) {
        @Override
        public void onLocationFound(Location location) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference(loc);
            myRef.setValue(latitude + "," + longitude);
        }

        @Override
        public void onTimeout() {

        }
    };
    tracker.startListening();

I really have no idea why it is not updating every 30 sec. Besides that, I had tried to drive and walk with my tracker app. But it still not updating the location to Firebase as the time and distance set in the settings.

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.