Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #2954 - Setting up specific toggle GPS functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bleege committed Dec 8, 2015
1 parent b2b2a00 commit e1ce7c4
Showing 1 changed file with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.mapbox.mapboxsdk.location;

import android.content.Context;
import android.location.Location;
import android.support.annotation.NonNull;
import com.mapzen.android.lost.api.LocationListener;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LostApiClient;

public class LocationServices {
public class LocationServices implements LocationListener {

private static LocationServices instance = null;

private LostApiClient mLocationClient;
private LocationRequest mLocationRequest;

private Location lastLocation = null;

/**
* Private constructor for singleton LocationServices
Expand All @@ -20,10 +23,6 @@ private LocationServices(Context context) {
super();
// Setup location services
mLocationClient = new LostApiClient.Builder(context).build();
mLocationRequest = LocationRequest.create()
.setFastestInterval(1000)
.setSmallestDisplacement(3.0f)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

/**
Expand All @@ -40,4 +39,58 @@ public static LocationServices getLocationServices(@NonNull final Context contex
}
return instance;
}

/**
* Enabled / Disable GPS focused location tracking
*
* @param enableGPS true if GPS is to be enabled, false if GPS is to be disabled
*/
public void toggleGPS(boolean enableGPS) {

if (enableGPS) {

if (mLocationClient.isConnected()) {
// Disconnect first to ensure that the new requests are GPS
com.mapzen.android.lost.api.LocationServices.FusedLocationApi.removeLocationUpdates(this);
mLocationClient.disconnect();
}

// Setup Fresh
mLocationClient.connect();
Location lastLocation = com.mapzen.android.lost.api.LocationServices.FusedLocationApi.getLastLocation();
if (lastLocation != null) {
this.lastLocation = lastLocation;
}

// LocationRequest Tuned for GPS
mLocationRequest = LocationRequest.create()
.setFastestInterval(1000)
.setSmallestDisplacement(3.0f)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

com.mapzen.android.lost.api.LocationServices.FusedLocationApi.requestLocationUpdates(mLocationRequest, this);


} else {

// Disconnect
if (mLocationClient.isConnected()) {
// Disconnect first to ensure that the new requests are GPS
com.mapzen.android.lost.api.LocationServices.FusedLocationApi.removeLocationUpdates(this);
mLocationClient.disconnect();
}

}


}

@Override
public void onLocationChanged(Location location) {
this.lastLocation = location;
}

public Location getLastLocation() {
return lastLocation;
}
}

0 comments on commit e1ce7c4

Please sign in to comment.