Skip to content

Instantly share code, notes, and snippets.

@sebastianknopf
Last active November 2, 2018 10:26
Show Gist options
  • Save sebastianknopf/73a22df7515603e58558d12a058ab953 to your computer and use it in GitHub Desktop.
Save sebastianknopf/73a22df7515603e58558d12a058ab953 to your computer and use it in GitHub Desktop.
custom current location overlay for osmdroid
package // ... ;
import org.osmdroid.views.overlay.Overlay;
final class CurrentLocationOverlay extends Overlay implements LocationListener {
private MapView mapView;
private LocationManager locationManager;
private boolean followLocation = true;
private String locationProvider;
private boolean isLocationAvailable;
public CurrentLocationOverlay(Context context, String locationProvider, MapView mapView) {
this.mapView = mapView;
this.locationProvider = locationProvider;
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onLocationChanged(Location location) {
this.lastLocation = location;
this.mapView.invalidate();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// do nothing ...
}
@Override
public void onProviderEnabled(String provider) {
if(this.locationProvider.equals(provider)) {
this.isLocationAvailable = true;
this.mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
if(this.locationProvider.equals(provider)) {
this.isLocationAvailable = false;
this.mapView.invalidate();
}
}
/*
* draw the current location symbol always when the overlay has been invalidated.
* the event is called thice: once for drawing the shadow and again for drawing the shape
*/
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if(!this.isEnabled() || !this.isLocationAvailable) { return; }
if(shadow) {
} else {
GeoPoint geoPoint = new GeoPoint(this.lastLocation.getLatitude(), this.lastLocation.getLongitude());
Projection projection = this.mapView.getProjection();
Point screenPoint = new Point();
projection.toPixels(geoPoint, screenPoint);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// draw outer circle
paint.setColor(Color.RED);
canvas.drawCircle(point.getX(), point.getY(), 20, paint);
// draw inner circle
paint.setColor(Color.RED);
canvas.drawCircle(point.getX(), point.getY(), 5, paint);
// center the map again if followLocation is enabled
if(this.followLocation) {
IMapController mapController = this.mapView.getController();
mapController.animateTo(geoPoint);
}
}
}
/*
* enable the location display on the map
*/
public void enableLocation() {
if(this.locationManager != null) {
this.isLocationAvailable = this.locationManager.isProviderEnabled(this.locationProvider);
this.locationManager.requestLocationUpdates(this.locationProvider, 100, 0.0f, this);
this.lastLocation = this.locationManager.getLastKnownLocation(this.locationProvider);
// enable drawing the overlay
this.setEnabled(true);
this.mapView.invalidate();
}
}
/*
* disable the location display on the map
*/
public void disableLocation() {
if(this.locationManager != null) {
this.locationManager.removeUpdates(this);
// prevent from drawing the overlay
this.setEnabled(false);
this.mapView.invalidate();
}
}
public void setFollowLocation(boolean followLocation) {
this.followLocation = followLocation;
}
public boolean getFollowLocation() {
return this.followLocation;
}
public void setLocationProvider(String locationProvider) {
this.disableLocation();
this.locationProvider = locationProvider;
this.enableLocation();
}
public String getLocationProvider() {
return this.locationProvider;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment