[android] #2954 - Setting up specific toggle GPS functionality
This commit is contained in:
@@ -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
|
||||
@@ -20,10 +23,6 @@ public class LocationServices {
|
||||
super();
|
||||
// Setup location services
|
||||
mLocationClient = new LostApiClient.Builder(context).build();
|
||||
mLocationRequest = LocationRequest.create()
|
||||
.setFastestInterval(1000)
|
||||
.setSmallestDisplacement(3.0f)
|
||||
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,4 +39,58 @@ public class LocationServices {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user