Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ext {
// mapbox
mapboxMapSdk : "com.mapbox.mapboxsdk:mapbox-android-sdk:${version.mapboxMapSdk}@aar",
mapboxServices : "com.mapbox.mapboxsdk:mapbox-android-services:${version.mapboxServices}",
mapboxGeoJson : "com.mapbox.mapboxsdk:mapbox-java-geojson:${version.mapboxServices}",
mapboxGeocoding : "com.mapbox.mapboxsdk:mapbox-java-services-geocoding:${version.mapboxGeocoding}",
geojson : "com.mapbox.mapboxsdk:mapbox-java-geojson:${version.mapboxServices}",

Expand Down
2 changes: 1 addition & 1 deletion plugin-geojson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
dependencies {
implementation dependenciesList.supportAppcompatV7
implementation dependenciesList.mapboxMapSdk
implementation dependenciesList.geojson
implementation dependenciesList.mapboxGeoJson
implementation dependenciesList.okhttp
implementation dependenciesList.timber
javadocDeps dependenciesList.mapboxMapSdk
Expand Down
7 changes: 7 additions & 0 deletions plugin-locationlayer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

Mapbox welcomes participation and contributions from everyone.

### 0.3.0 - December 1, 2017
- Change accuracy ring from fill layer to a circle layer [#186](https://github.com/mapbox/mapbox-plugins-android/pull/186)
- Adds last known location API [#199](https://github.com/mapbox/mapbox-plugins-android/pull/199)
- Adds location layer icon click listener [#198](https://github.com/mapbox/mapbox-plugins-android/pull/198)
- Rework location layer abstraction [#196](https://github.com/mapbox/mapbox-plugins-android/pull/196)

### 0.2.0 - August 18, 2017
- Updated Map and Mapbox Java Dependencies [#84](https://github.com/mapbox/mapbox-plugins-android/pull/84)
- Navigation icon now uses runtime styling to scale at lower zoom levels [#84](https://github.com/mapbox/mapbox-plugins-android/pull/84)
- Added listener for compass heading and accuracy changes [#84](https://github.com/mapbox/mapbox-plugins-android/pull/84)

### 0.1.0 - July 17, 2017
- Initial release as a standalone package.

10 changes: 8 additions & 2 deletions plugin-locationlayer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ android {

dependencies {
implementation dependenciesList.supportAppcompatV7
implementation dependenciesList.mapboxServices
implementation dependenciesList.mapboxMapSdk

implementation(dependenciesList.mapboxMapSdk) {
transitive = true
}
implementation dependenciesList.mapboxGeoJson

// Timber
implementation dependenciesList.timber

// Unit testing
testImplementation dependenciesList.junit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.res.TypedArray;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
Expand All @@ -13,8 +14,9 @@
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.style.functions.Function;
import com.mapbox.mapboxsdk.style.layers.FillLayer;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.Property;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
Expand All @@ -27,7 +29,6 @@
import java.util.Map;

import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.ACCURACY_LAYER;
import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.ACCURACY_SOURCE;
import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.BACKGROUND_ICON;
import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.BACKGROUND_LAYER;
import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.BEARING_ICON;
Expand All @@ -43,8 +44,12 @@
import static com.mapbox.mapboxsdk.style.layers.Property.ICON_ROTATION_ALIGNMENT_MAP;
import static com.mapbox.mapboxsdk.style.layers.Property.NONE;
import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circlePitchAlignment;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleStrokeColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleStrokeWidth;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
Expand Down Expand Up @@ -79,7 +84,6 @@ private void addLayers() {

private void addSources() {
addSource(LOCATION_SOURCE);
addSource(ACCURACY_SOURCE);
}

void applyStyle(int styleRes) {
Expand All @@ -100,7 +104,9 @@ void applyStyle(int styleRes) {

float accuracyAlpha = typedArray.getFloat(R.styleable.LocationLayer_accuracyAlpha, 0.15f);
if (accuracyAlpha < 0 || accuracyAlpha > 1) {
throw new RuntimeException("Location layer accuracy alpha value must be between 0.0 and 1.0.");
throw new UnsupportedOperationException(
"Location layer accuracy alpha value must be between 0.0 and 1.0."
);
}
int accuracyColor = typedArray.getColor(R.styleable.LocationLayer_accuracyColor,
ContextCompat.getColor(context, R.color.mapbox_plugin_location_layer_blue));
Expand Down Expand Up @@ -157,8 +163,32 @@ private void addNavigationLayer() {
}

private void addAccuracyLayer() {
FillLayer accuracyLayer = new FillLayer(ACCURACY_LAYER, ACCURACY_SOURCE);
addLayerToMap(accuracyLayer, BACKGROUND_LAYER);
CircleLayer locationAccuracyLayer = new CircleLayer(
LocationLayerConstants.ACCURACY_LAYER, LocationLayerConstants.LOCATION_SOURCE);
addLayerToMap(locationAccuracyLayer, LocationLayerConstants.BACKGROUND_LAYER);
}

void setAccuracy(Location location) {
CircleLayer accuracyLayer = (CircleLayer) mapboxMap.getLayer(LocationLayerConstants.ACCURACY_LAYER);
if (accuracyLayer != null) {
accuracyLayer.setProperties(
circleRadius(calculateZoomLevelRadius(location))
);
}
}

void updateAccuracyRadius(Location location) {
CircleLayer accuracyLayer = (CircleLayer) mapboxMap.getLayer(LocationLayerConstants.ACCURACY_LAYER);
if (accuracyLayer != null && accuracyLayer.getVisibility().isValue()) {
accuracyLayer.setProperties(
circleRadius(calculateZoomLevelRadius(location))
);
}
}

private float calculateZoomLevelRadius(Location location) {
double metersPerPixel = mapboxMap.getProjection().getMetersPerPixelAtLatitude(location.getLatitude());
return (float) (location.getAccuracy() * (1 / metersPerPixel));
}

//
Expand All @@ -169,10 +199,6 @@ void setLocationPoint(Point locationPoint) {
sourceMap.get(LOCATION_SOURCE).setGeoJson(locationPoint);
}

void setAccuracy(FeatureCollection accuracy) {
sourceMap.get(ACCURACY_SOURCE).setGeoJson(accuracy);
}

private void addSource(String sourceId) {
FeatureCollection emptyFeature = FeatureCollection.fromFeatures(new Feature[] {});
GeoJsonSource locationSource = new GeoJsonSource(
Expand Down Expand Up @@ -219,8 +245,11 @@ private void styleBearing(Drawable bearingDrawable) {

private void styleAccuracy(float accuracyAlpha, @ColorInt int accuracyColor) {
layerMap.get(ACCURACY_LAYER).setProperties(
fillColor(accuracyColor),
fillOpacity(accuracyAlpha)
circleColor(accuracyColor),
circleOpacity(accuracyAlpha),
circlePitchAlignment(Property.CIRCLE_PITCH_ALIGNMENT_MAP),
circleStrokeWidth(0.5f),
circleStrokeColor(accuracyColor)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*/
class LocationLayerConstants {

private LocationLayerConstants() {
// Class should not be initialized
}

// Controls the compass update rate in milliseconds
static final int COMPASS_UPDATE_RATE_MS = 500;

Expand All @@ -16,7 +20,6 @@ class LocationLayerConstants {

// Sources
static final String LOCATION_SOURCE = "mapbox-location-source";
static final String ACCURACY_SOURCE = "mapbox-location-accuracy-source";

// Layers
static final String FOREGROUND_LAYER = "mapbox-location-layer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
import com.mapbox.services.api.utils.turf.TurfConstants;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Point;
import com.mapbox.services.commons.geojson.Polygon;
import com.mapbox.services.commons.models.Position;

import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.ACCURACY_LAYER;
import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.BEARING_LAYER;
Expand All @@ -49,15 +44,13 @@
* @since 0.1.0
*/
public class LocationLayerPlugin implements LocationEngineListener, CompassListener, MapView.OnMapChangedListener,
LifecycleObserver, MapboxMap.OnMapClickListener {

private static final int ACCURACY_CIRCLE_STEPS = 48;
LifecycleObserver, MapboxMap.OnCameraMoveListener, MapboxMap.OnMapClickListener {

private LocationLayer locationLayer;
private CompassManager compassManager;
private LocationEngine locationEngine;
private MapboxMap mapboxMap;
private MapView mapView;
private final MapboxMap mapboxMap;
private final MapView mapView;

// Enabled booleans
@LocationLayerMode.Mode
Expand All @@ -75,11 +68,15 @@ public class LocationLayerPlugin implements LocationEngineListener, CompassListe
private long locationUpdateTimestamp;
private boolean linearAnimation;


private Location location;

private OnLocationLayerClickListener onLocationLayerClickListener;

@StyleRes
private int styleRes;


/**
* Construct a {@code LocationLayerPlugin}
*
Expand Down Expand Up @@ -146,6 +143,8 @@ public void setLocationLayerEnabled(@LocationLayerMode.Mode int locationLayerMod
locationEngine.addLocationEngineListener(this);
}

mapboxMap.addOnCameraMoveListener(this);

if (locationLayerMode == LocationLayerMode.COMPASS) {
setLinearAnimation(false);
setNavigationEnabled(false);
Expand Down Expand Up @@ -250,7 +249,9 @@ public void onStop() {
if (locationEngine != null) {
locationEngine.removeLocationEngineListener(this);
}

if (mapboxMap != null) {
mapboxMap.removeOnCameraMoveListener(this);
}
}

/**
Expand All @@ -266,10 +267,13 @@ public void onStart() {
setLocationLayerEnabled(locationLayerMode);
}

if (compassManager.getCompassListeners().size() > 0
|| locationLayerMode == LocationLayerMode.COMPASS && compassManager.isSensorAvailable()) {
if (!compassManager.getCompassListeners().isEmpty()
|| (locationLayerMode == LocationLayerMode.COMPASS && compassManager.isSensorAvailable())) {
compassManager.onStart();
}
if (mapboxMap != null) {
mapboxMap.addOnCameraMoveListener(this);
}
}

/**
Expand Down Expand Up @@ -315,7 +319,7 @@ public void addCompassListener(@NonNull CompassListener compassListener) {
*/
public void removeCompassListener(@Nullable CompassListener compassListener) {
compassManager.removeCompassListener(compassListener);
if (compassManager.getCompassListeners().size() < 1) {
if (compassManager.getCompassListeners().isEmpty()) {
compassManager.onStop();
}
}
Expand Down Expand Up @@ -370,7 +374,7 @@ private void updateLocation(Location location) {
if (locationLayerMode == LocationLayerMode.NAVIGATION && location.hasBearing()) {
bearingChangeAnimate(location.getBearing());
} else if (locationLayerMode != LocationLayerMode.NAVIGATION) {
setAccuracy(location);
locationLayer.setAccuracy(location);
}
setLocation(location);
}
Expand All @@ -393,7 +397,7 @@ private void setLastLocation() {
Location lastLocation = locationEngine.getLastLocation();
if (lastLocation != null) {
setLocation(lastLocation);
setAccuracy(lastLocation);
locationLayer.setAccuracy(lastLocation);
}
}

Expand Down Expand Up @@ -449,7 +453,7 @@ private void setMyBearingEnabled(boolean bearingEnabled) {
if (bearingEnabled) {
compassManager.onStart();
} else {
if (compassManager != null && compassManager.getCompassListeners().size() < 1) {
if (compassManager != null && compassManager.getCompassListeners().isEmpty()) {
compassManager.onStop();
}
}
Expand All @@ -474,26 +478,9 @@ private void setNavigationLayerVisibility(boolean visible) {
locationLayer.setLayerVisibility(NAVIGATION_LAYER, visible);
}

/**
* Using the {@link Location#getAccuracy()} provided when a new location comes in, the source is updated to reflect
* the new value
*
* @param location the latest user location
* @since 0.1.0
*/
private void setAccuracy(Location location) {
// TODO replace fill-layer with circle-layer once circle-pitch-alignment is supported in Runtime Styling
// https://github.com/mapbox/mapbox-gl-js/issues/4120
Position userPosition = Position.fromLngLat(location.getLongitude(), location.getLatitude());
Polygon accuracyCircle = TurfTransformation.circle(
userPosition, location.getAccuracy(), ACCURACY_CIRCLE_STEPS, TurfConstants.UNIT_METERS);

// Create new GeoJson object using the new accuracy circle created.
FeatureCollection featureCollection = FeatureCollection.fromFeatures(
new Feature[] {Feature.fromGeometry(accuracyCircle)}
);

locationLayer.setAccuracy(featureCollection);
@Override
public void onCameraMove() {
locationLayer.updateAccuracyRadius(location);
}

/**
Expand Down

This file was deleted.