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
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;

import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEnginePriority;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
Expand All @@ -27,11 +24,8 @@ public class LocationLayerMapChangeActivity extends AppCompatActivity implements
FloatingActionButton stylesFab;

private LocationLayerPlugin locationPlugin;
private LocationEngine locationEngine;
private MapboxMap mapboxMap;

private boolean customStyle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -46,11 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.setFastestInterval(1000);
locationEngine.activate();
locationPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationPlugin = new LocationLayerPlugin(mapView, mapboxMap);
locationPlugin.setRenderMode(RenderMode.COMPASS);
getLifecycle().addObserver(locationPlugin);
}
Expand All @@ -67,9 +57,6 @@ public void onStyleFabClick() {
protected void onStart() {
super.onStart();
mapView.onStart();
if (locationEngine != null) {
locationEngine.requestLocationUpdates();
}
}

@Override
Expand All @@ -87,9 +74,6 @@ protected void onPause() {
@Override
protected void onStop() {
super.onStop();
if (locationEngine != null) {
locationEngine.removeLocationUpdates();
}
mapView.onStop();
}

Expand All @@ -103,9 +87,6 @@ protected void onSaveInstanceState(Bundle outState) {
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
if (locationEngine != null) {
locationEngine.deactivate();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
import static com.mapbox.mapboxsdk.plugins.locationlayer.Utils.generateShadow;
import static com.mapbox.mapboxsdk.plugins.locationlayer.Utils.getBitmapFromDrawable;
import static com.mapbox.mapboxsdk.plugins.locationlayer.Utils.getDrawable;
import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import static com.mapbox.mapboxsdk.style.expressions.Expression.linear;
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
import static com.mapbox.mapboxsdk.style.expressions.Expression.match;
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
Expand Down Expand Up @@ -121,6 +121,7 @@ void applyStyle(@NonNull LocationLayerOptions options) {
styleBackground(options);
styleBearing(options);
styleAccuracy(options.accuracyAlpha(), options.accuracyColor());
styleScaling(options);
}

void setRenderMode(@RenderMode.Mode int renderMode) {
Expand Down Expand Up @@ -196,12 +197,6 @@ private void addSymbolLayer(String layerId, String beforeLayerId) {
layer.setProperties(
iconAllowOverlap(true),
iconIgnorePlacement(true),
iconSize(
interpolate(exponential(1f), zoom(),
stop(0f, 0.6f),
stop(18f, 1.2f)
)
),
iconRotationAlignment(ICON_ROTATION_ALIGNMENT_MAP),
iconRotate(
match(literal(layerId), literal(0f),
Expand Down Expand Up @@ -383,6 +378,21 @@ private void styleForegroundGPS(LocationLayerOptions options) {
getDrawable(context, options.gpsDrawable(), options.foregroundStaleTintColor()));
}

private void styleScaling(LocationLayerOptions options) {
for (Layer layer : layerMap.values()) {
if (layer instanceof SymbolLayer) {
layer.setProperties(
iconSize(
interpolate(linear(), zoom(),
stop(options.minZoom(), options.minZoomIconScale()),
stop(options.maxZoom(), options.maxZoomIconScale())
)
)
);
}
}
}

void setLocationsStale(boolean isStale) {
// If options has stale state disabled, just return here.
if (!options.enableStaleState()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public abstract class LocationLayerOptions implements Parcelable {
*/
private static final float MIN_ZOOM_DEFAULT = 2;

/**
* Default icon scale factor when the map is zoomed out
*/
private static final float MIN_ZOOM_ICON_SCALE_DEFAULT = 0.6f;

/**
* Default icon scale factor when the map is zoomed in
*/
private static final float MAX_ZOOM_ICON_SCALE_DEFAULT = 1f;

/**
* Default map padding
*/
Expand Down Expand Up @@ -162,6 +172,13 @@ public static LocationLayerOptions createFromAttributes(@NonNull Context context
builder.maxZoom(maxZoom);
builder.minZoom(minZoom);

float minScale = typedArray.getFloat(
R.styleable.mapbox_LocationLayer_mapbox_minZoomIconScale, MIN_ZOOM_ICON_SCALE_DEFAULT);
float maxScale = typedArray.getFloat(
R.styleable.mapbox_LocationLayer_mapbox_maxZoomIconScale, MAX_ZOOM_ICON_SCALE_DEFAULT);
builder.minZoomIconScale(minScale);
builder.maxZoomIconScale(maxScale);

typedArray.recycle();

return builder.build();
Expand Down Expand Up @@ -200,6 +217,8 @@ private static Builder builder() {
.staleStateTimeout(STALE_STATE_DELAY_MS)
.maxZoom(MAX_ZOOM_DEFAULT)
.minZoom(MIN_ZOOM_DEFAULT)
.maxZoomIconScale(MAX_ZOOM_ICON_SCALE_DEFAULT)
.minZoomIconScale(MIN_ZOOM_ICON_SCALE_DEFAULT)
.padding(PADDING_DEFAULT);
}

Expand Down Expand Up @@ -405,6 +424,24 @@ private static Builder builder() {
*/
public abstract double minZoom();

/**
* The scale factor of the location icon when the map is zoomed in. Based on {@link #maxZoom()}.
* Scaling is linear.
*
* @return icon scale factor
* @since 0.6.0
*/
public abstract float maxZoomIconScale();

/**
* The scale factor of the location icon when the map is zoomed out. Based on {@link #minZoom()}.
* Scaling is linear.
*
* @return icon scale factor
* @since 0.6.0
*/
public abstract float minZoomIconScale();

/**
* Minimum single pointer movement in pixels required to break camera tracking.
*
Expand Down Expand Up @@ -627,10 +664,35 @@ public abstract Builder maxZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM,
* Sets the minimum zoom level the map can be displayed at.
*
* @param minZoom The new minimum zoom level.
* @since 0.5.0
*/
public abstract Builder minZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM,
to = MapboxConstants.MAXIMUM_ZOOM) double minZoom);

/**
* Sets the scale factor of the location icon when the map is zoomed in. Based on {@link #maxZoom()}.
* Scaling is linear and the new pixel size of the image will be the original pixel size multiplied by the argument.
* <p>
* Set both this and {@link #minZoomIconScale(float)} to 1f to disable location icon scaling.
* </p>
*
* @param maxZoomIconScale icon scale factor
* @since 0.6.0
*/
public abstract Builder maxZoomIconScale(float maxZoomIconScale);

/**
* Sets the scale factor of the location icon when the map is zoomed out. Based on {@link #maxZoom()}.
* Scaling is linear and the new pixel size of the image will be the original pixel size multiplied by the argument.
* <p>
* Set both this and {@link #maxZoomIconScale(float)} to 1f to disable location icon scaling.
* </p>
*
* @param minZoomIconScale icon scale factor
* @since 0.6.0
*/
public abstract Builder minZoomIconScale(float minZoomIconScale);

/**
* Sets minimum single pointer movement (map pan) in pixels required to break camera tracking.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<public name="mapbox_maxZoom" format="float" type="attr"/>
<public name="mapbox_minZoom" format="float" type="attr"/>

<!-- Icon scale based on map zoom levels -->
<public name="mapbox_maxZoomIconScale" format="float" type="attr"/>
<public name="mapbox_minZoomIconScale" format="float" type="attr"/>

<!-- Camera tracking settings -->
<public name="mapbox_trackingInitialMoveThreshold" format="float" type="attr"/>
<public name="mapbox_trackingMultiFingerMoveThreshold" format="float" type="attr"/>
Expand Down
4 changes: 4 additions & 0 deletions plugin-locationlayer/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<attr name="mapbox_maxZoom" format="float"/>
<attr name="mapbox_minZoom" format="float"/>

<!-- Icon scale based on map zoom levels -->
<attr name="mapbox_maxZoomIconScale" format="float"/>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These also need to be publically exposed in the public_attrs.xml file.

<attr name="mapbox_minZoomIconScale" format="float"/>

<!-- Camera tracking settings -->
<attr name="mapbox_trackingInitialMoveThreshold" format="dimension"/>
<attr name="mapbox_trackingMultiFingerMoveThreshold" format="dimension"/>
Expand Down
4 changes: 4 additions & 0 deletions plugin-locationlayer/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<item name="mapbox_maxZoom">18</item>
<item name="mapbox_minZoom">2</item>

<!-- Location icon scale based on map zoom levels -->
<item name="mapbox_maxZoomIconScale">1</item>
<item name="mapbox_minZoomIconScale">0.6</item>

<!-- Camera tracking settings -->
<item name="mapbox_trackingInitialMoveThreshold">
@dimen/mapbox_locationLayerTrackingInitialMoveThreshold
Expand Down