/LOST

A drop-in replacement for Google Play Services Location APIs

Primary LanguageJavaOtherNOASSERTION

LOST

Circle CI Build Status

Location Open Source Tracker for Android

Usage

LOST is a drop-in replacement for Google Play Services FusedLocationProviderApi that makes calls directly to the LocationManger.

This project seeks to provide an open source alternative to the Fused Location Provider that depends only on the Android SDK. Operations supported at this time include getting the last known location and registering for location updates.

Connecting to the LOST API Client

When using LOST, GoogleApiClient is replaced by LostApiClient. Connecting to LOST is even easier since there are no ConnectionCallbacks or OnConnectionFailedListener objects to manage.

LostApiClient lostApiClient = new LostApiClient.Builder(this).build();
lostApiClient.connect();

LOST instantly connects to the LocationManager and can immediately retrieve that last known location or begin sending location updates.

Getting the Last Known Location

Once connected you can request the last known location. The actual logic to determine the best most recent location is based this classic blog post by Reto Meier.

Location location = LocationServices.FusedLocationApi.getLastLocation();
if (location != null) {
  // Do stuff
}

Requesting Location Updates

LOST also provides the ability to request ongoing location updates. You can specify the update interval, minimum displacement, and priority. The priority determines which location providers will be activated.

LocationRequest request = LocationRequest.create()
    .setInterval(5000)
    .setSmallestDisplacement(10)
    .setPriority(LocationRequest.PRIORITY_LOW_POWER);

LocationListener listener = new LocationListener() {
  @Override
  public void onLocationChanged(Location location) {
    // Do stuff
  }
};

LocationServices.FusedLocationApi.requestLocationUpdates(request, listener);

Currently location updates can only be requested with a LocationListener object. In the future we are planning to add location updates via a PendingIntent as well.

Mock Locations

With LOST you can mock not just individual locations but also entire routes. By loading a GPX trace file onto the device you can configure LOST to replay locations from the trace file including latitude, longitude, speed, and bearing.

Mocking a single location

To mock a single location with LOST you must first enable mock mode. Then you simply create a mock location object and pass it to the API.

Location mockLocation = new Location("mock");
mockLocation.setLatitude(40.7484);
mockLocation.setLongitude(-73.9857);
LocationServices.FusedLocationApi.setMockMode(true);
LocationServices.FusedLocationApi.setMockLocation(mockLocation);

The mock location object you set will be immediately returned to all registered listeners and will be returned the next time getLastLocation() is called.

Mocking an entire route

To mock an entire route you must first transfer a GPX trace file to the device using adb. Sample GPX traces can be found on the public GPS traces page for OpenStreetMap. Once the trace file is loaded on the device you can tell LOST to replay the locations in the trace at the requested update interval.

File file = new File(Environment.getExternalStorageDirectory(), "mock_track.gpx");
LocationServices.FusedLocationApi.setMockMode(true);
LocationServices.FusedLocationApi.setMockTrace(file);

For more in-depth examples, please refer to the sample application.

Install

Download Jar

Download the latest JAR.

Maven

Include dependency using Maven.

<dependency>
  <groupId>com.mapzen.android</groupId>
  <artifactId>lost</artifactId>
  <version>1.0.1</version>
</dependency>

Gradle

Include dependency using Gradle.

compile 'com.mapzen.android:lost:1.0.1'