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

Commit

Permalink
[android] #2954 - Building LocationListener infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
bleege committed Dec 9, 2015
1 parent cddedf6 commit 220006f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mapbox.mapboxsdk.location;

import android.location.Location;

public interface LocationListener {

/**
* Callback method for receiving location updates from LocationServices.
* @param location The new Location data
*/
public void onLocationChanged(Location location);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
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;
import java.util.ArrayList;
import java.util.List;

public class LocationServices implements LocationListener {
public class LocationServices implements com.mapzen.android.lost.api.LocationListener {

private static LocationServices instance = null;

Expand All @@ -16,13 +17,16 @@ public class LocationServices implements LocationListener {

private Location lastLocation = null;

private List<LocationListener> locationListeners = null;

/**
* Private constructor for singleton LocationServices
*/
private LocationServices(Context context) {
super();
// Setup location services
mLocationClient = new LostApiClient.Builder(context).build();
locationListeners = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -88,9 +92,35 @@ public void toggleGPS(boolean enableGPS) {
@Override
public void onLocationChanged(Location location) {
this.lastLocation = location;

// Update Listeners
for (LocationListener listener : this.locationListeners) {
listener.onLocationChanged(location);
}
}

/**
* Last known location
* @return Last known location data
*/
public Location getLastLocation() {
return lastLocation;
}

/**
* Registers a LocationListener to receive location updates
* @param locationListener LocationListener
*/
public void addLocationListener(@NonNull LocationListener locationListener) {
this.locationListeners.add(locationListener);
}

/**
* Unregister a LocationListener to stop receiving location updates
* @param locationListener LocationListener to remove
* @return True if LocationListener was found and removed, False if it was not
*/
public boolean removeLocationListener(@NonNull LocationListener locationListener) {
return this.locationListeners.remove(locationListener);
}
}

0 comments on commit 220006f

Please sign in to comment.