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

Commit

Permalink
[android] #2954 - Refactoring UserLocationView to use common Location…
Browse files Browse the repository at this point in the history
…Services for location data
  • Loading branch information
bleege committed Dec 9, 2015
1 parent 4c19688 commit 2769744
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class LocationServices implements com.mapzen.android.lost.api.LocationLis

private List<LocationListener> locationListeners = null;

private boolean isGPSEnabled = false;

/**
* Private constructor for singleton LocationServices
*/
Expand Down Expand Up @@ -74,7 +76,6 @@ public void toggleGPS(boolean enableGPS) {

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


} else {

// Disconnect
Expand All @@ -86,7 +87,11 @@ public void toggleGPS(boolean enableGPS) {

}

isGPSEnabled = enableGPS;
}

public boolean isGPSEnabled() {
return isGPSEnabled;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapzen.android.lost.api.LocationListener;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LocationServices;
import com.mapzen.android.lost.api.LostApiClient;
import com.mapbox.mapboxsdk.location.LocationListener;
import com.mapbox.mapboxsdk.location.LocationServices;

final class UserLocationView extends View {
final class UserLocationView extends View implements LocationListener {

private MapView mMapView;

Expand Down Expand Up @@ -80,10 +77,7 @@ final class UserLocationView extends View {
private ObjectAnimator mMarkerAccuracyAnimator;

private boolean mPaused = false;
private LostApiClient mLocationClient;
private LocationRequest mLocationRequest;
private Location mUserLocation;
private MyLocationListener mLocationListener;

MapView.OnMyLocationChangeListener mOnMyLocationChangeListener;

Expand Down Expand Up @@ -122,13 +116,6 @@ private void initialize(Context context) {
ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(lp);

// Setup location services
mLocationClient = new LostApiClient.Builder(getContext()).build();
mLocationRequest = LocationRequest.create()
.setFastestInterval(1000)
.setSmallestDisplacement(3.0f)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

// Setup sensors
mBearingChangeListener = new MyBearingListener(context);

Expand Down Expand Up @@ -338,29 +325,27 @@ public Location getLocation() {
* @param enableGps true if GPS is to be enabled, false if GPS is to be disabled
*/
private void toggleGps(boolean enableGps) {
if (mLocationClient == null) {
return;
}

LocationServices locationServices = LocationServices.getLocationServices(getContext());

if (enableGps) {
if (!mLocationClient.isConnected()) {
mUserLocation = null;
mLocationClient.connect();
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation();
if (lastLocation != null) {
setLocation(lastLocation);
}
mLocationListener = new MyLocationListener();
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationRequest, mLocationListener);
// Set an initial location if one available
Location lastLocation = locationServices.getLastLocation();
if (lastLocation != null) {
setLocation(lastLocation);
}

// Register for Location Updates
locationServices.addLocationListener(this);
} else {
if (mLocationClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mLocationListener);
mLocationListener = null;
mLocationClient.disconnect();
mUserLocation = null;
}
// Disable location and user dot
setLocation(null);

// Deregister for Location Updates
locationServices.removeLocationListener(this);
}

locationServices.toggleGPS(enableGps);
}

public void setMyBearingTrackingMode(@MyBearingTracking.Mode int myBearingTrackingMode) {
Expand Down Expand Up @@ -471,15 +456,17 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}


private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (mPaused) {
return;
}
setLocation(location);
/**
* Callback method for receiving location updates from LocationServices.
*
* @param location The new Location data
*/
@Override
public void onLocationChanged(Location location) {
if (mPaused) {
return;
}
setLocation(location);
}

private boolean isStale(Location location) {
Expand Down Expand Up @@ -627,11 +614,17 @@ void updateOnNextFrame() {
mMapView.update();
}

/**
* Called from MapView.onPause()
*/
public void pause() {
mPaused = true;
toggleGps(false);
}

/**
* Called from MapView.onResume()
*/
public void resume() {
mPaused = false;
if (isEnabled()) {
Expand Down

0 comments on commit 2769744

Please sign in to comment.