Skip to content

Commit

Permalink
Gestures logic for camera tracking, new telemetry library (#327)
Browse files Browse the repository at this point in the history
* [location-layer-plugin] - bumped maps SDK to 6.x

* [location-layer-plugin] - style options initialization fixes

* [location-layer-plugin] - added gestures handling implementation
  • Loading branch information
Łukasz Paczos authored and Cameron Mace committed Mar 12, 2018
1 parent a29e25b commit c149689
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import android.widget.TextView;
import android.widget.Toast;

import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.services.android.telemetry.permissions.PermissionsListener;
import com.mapbox.services.android.telemetry.permissions.PermissionsManager;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.locationlayer.CompassListener;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LostLocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineProvider;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -39,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onMapReady(final MapboxMap mapboxMap) {
LocationEngine locationEngine = new LostLocationEngine(this);
LocationEngine locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationLayerPlugin.setRenderMode(RenderMode.COMPASS);
locationLayerPlugin.addCompassListener(new CompassListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.mapboxsdk.plugins.testapp.Utils;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEnginePriority;
import com.mapbox.services.android.telemetry.location.LostLocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineProvider;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand Down Expand Up @@ -48,11 +48,12 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
locationEngine = LostLocationEngine.getLocationEngine(this);
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
locationPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationPlugin.setRenderMode(RenderMode.COMPASS);
getLifecycle().addObserver(locationPlugin);
}

@OnClick(R.id.fabStyles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerOptions;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.OnCameraTrackingChangedListener;
import com.mapbox.mapboxsdk.plugins.locationlayer.OnLocationLayerClickListener;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
import com.mapbox.services.android.telemetry.location.LocationEnginePriority;
import com.mapbox.services.android.telemetry.location.LostLocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineProvider;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,7 +39,7 @@
import butterknife.OnClick;

public class LocationLayerModesActivity extends AppCompatActivity implements OnMapReadyCallback,
LocationEngineListener, OnLocationLayerClickListener {
LocationEngineListener, OnLocationLayerClickListener, OnCameraTrackingChangedListener {

@BindView(R.id.map_view)
MapView mapView;
Expand Down Expand Up @@ -86,12 +88,16 @@ public void locationModeCompass(View view) {
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
locationEngine = new LostLocationEngine(this);
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.addLocationEngineListener(this);
locationEngine.activate();
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
LocationLayerOptions options = LocationLayerOptions.builder(this)
.padding(new int[] {0, 650, 0, 0})
.build();
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine, options);
locationLayerPlugin.addOnLocationClickListener(this);
locationLayerPlugin.addOnCameraTrackingChangedListener(this);
getLifecycle().addObserver(locationLayerPlugin);
}

Expand Down Expand Up @@ -261,4 +267,14 @@ private void showTrackingListDialog() {
});
listPopup.show();
}

@Override
public void onCameraTrackingDismissed() {
locationTrackingBtn.setText("None");
}

@Override
public void onCameraTrackingChanged(int currentMode) {
// do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineListener;
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;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.mapboxsdk.plugins.testapp.Utils;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
import com.mapbox.services.android.telemetry.location.LocationEnginePriority;
import com.mapbox.services.android.telemetry.location.LostLocationEngine;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void manualLocationChangeFabClick(View view) {
@SuppressWarnings( {"MissingPermission"})
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
locationEngine = new LostLocationEngine(this);
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.addLocationEngineListener(this);
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext {
]

version = [
mapboxMapSdk : '5.5.0',
mapboxMapSdk : '6.0.0-20180301.094208-178',
mapboxGeocoding : '3.0.0-beta.3',
mapboxGeoJson : '3.0.0-beta.3',
mapboxServices : '2.2.9',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
Expand All @@ -19,9 +22,6 @@
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Point;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -78,11 +78,11 @@ final class LocationLayer implements LocationLayerAnimator.OnAnimationsValuesCha
LocationLayer(MapView mapView, MapboxMap mapboxMap, LocationLayerOptions options) {
this.mapboxMap = mapboxMap;
this.context = mapView.getContext();
initializeComponents();
initializeComponents(options);
setRenderMode(RenderMode.NORMAL);
}

void initializeComponents() {
void initializeComponents(LocationLayerOptions options) {
addLocationSource();
addLayers();
applyStyle(options);
Expand Down Expand Up @@ -320,7 +320,7 @@ boolean onMapClick(LatLng point) {

@Override
public void onNewLatLngValue(LatLng latLng) {
Point point = Point.fromCoordinates(new double[] {latLng.getLongitude(), latLng.getLatitude()});
Point point = Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude());
setLocationPoint(point);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
package com.mapbox.mapboxsdk.plugins.locationlayer;

import android.graphics.PointF;

import com.mapbox.android.gestures.MoveGestureDetector;
import com.mapbox.android.gestures.RotateGestureDetector;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode;

public class LocationLayerCamera implements LocationLayerAnimator.OnAnimationsValuesChangeListener {
final class LocationLayerCamera implements LocationLayerAnimator.OnAnimationsValuesChangeListener {

@CameraMode.Mode
private int cameraMode;

private MapboxMap mapboxMap;
private final MapboxMap mapboxMap;
private final OnCameraTrackingChangedListener internalCameraTrackingChangedListener;
private LocationLayerOptions options;
private boolean adjustFocalPoint;

private final MoveGestureDetector moveGestureDetector;

public LocationLayerCamera(MapboxMap mapboxMap) {
LocationLayerCamera(
MapboxMap mapboxMap,
OnCameraTrackingChangedListener internalCameraTrackingChangedListener,
LocationLayerOptions options) {
this.mapboxMap = mapboxMap;
this.internalCameraTrackingChangedListener = internalCameraTrackingChangedListener;
initializeOptions(options);

moveGestureDetector = mapboxMap.getGesturesManager().getMoveGestureDetector();
mapboxMap.addOnMoveListener(onMoveListener);
mapboxMap.addOnRotateListener(onRotateListener);
}

void initializeOptions(LocationLayerOptions options) {
this.options = options;
}

public void setCameraMode(@CameraMode.Mode int cameraMode) {
void setCameraMode(@CameraMode.Mode int cameraMode) {
boolean wasTracking = isLocationTracking();
this.cameraMode = cameraMode;
mapboxMap.cancelTransitions();
adjustGesturesThresholds();
notifyCameraTrackingChangeListener(wasTracking);
}

public int getCameraMode() {
int getCameraMode() {
return cameraMode;
}

private void setBearing(float bearing) {
mapboxMap.setBearing(bearing);
mapboxMap.moveCamera(CameraUpdateFactory.bearingTo(bearing));
}

private void setLatLng(LatLng latLng) {
mapboxMap.setLatLng(latLng);
mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}

@Override
Expand All @@ -40,6 +66,12 @@ public void onNewLatLngValue(LatLng latLng) {
|| cameraMode == CameraMode.TRACKING_GPS_NORTH) {
setLatLng(latLng);
}

if (adjustFocalPoint) {
PointF focalPoint = mapboxMap.getProjection().toScreenLocation(latLng);
mapboxMap.getUiSettings().setFocalPoint(focalPoint);
adjustFocalPoint = false;
}
}

@Override
Expand All @@ -61,9 +93,88 @@ public void onNewCompassBearingValue(float compassBearing) {
setBearing(compassBearing);
}
}
}

private void adjustGesturesThresholds() {
if (isLocationTracking()) {
adjustFocalPoint = true;
moveGestureDetector.setMoveThreshold(options.trackingInitialMoveThreshold());
}
}

private boolean isLocationTracking() {
return cameraMode == CameraMode.TRACKING
|| cameraMode == CameraMode.TRACKING_COMPASS
|| cameraMode == CameraMode.TRACKING_GPS
|| cameraMode == CameraMode.TRACKING_GPS_NORTH;
}

private boolean isBearingTracking() {
return cameraMode == CameraMode.NONE_COMPASS
|| cameraMode == CameraMode.TRACKING_COMPASS
|| cameraMode == CameraMode.NONE_GPS
|| cameraMode == CameraMode.TRACKING_GPS
|| cameraMode == CameraMode.TRACKING_GPS_NORTH;
}

private void notifyCameraTrackingChangeListener(boolean wasTracking) {
internalCameraTrackingChangedListener.onCameraTrackingChanged(cameraMode);
if (wasTracking && !isLocationTracking()) {
mapboxMap.getUiSettings().setFocalPoint(null);
moveGestureDetector.setMoveThreshold(moveGestureDetector.getDefaultMoveThreshold());
internalCameraTrackingChangedListener.onCameraTrackingDismissed();
}
}

private MapboxMap.OnMoveListener onMoveListener = new MapboxMap.OnMoveListener() {
private boolean interrupt;

@Override
public void onMoveBegin(MoveGestureDetector detector) {
if (detector.getPointersCount() > 1
&& detector.getMoveThreshold() != options.trackingMultiFingerMoveThreshold()
&& isLocationTracking()) {
detector.setMoveThreshold(options.trackingMultiFingerMoveThreshold());
interrupt = true;
}
}

/*
@Override
public void onMove(MoveGestureDetector detector) {
if (interrupt) {
detector.interrupt();
return;
}

float targetBearing = Utils.shortestRotation(0, (float) bearing);*/
if (isLocationTracking()) {
setCameraMode(CameraMode.NONE);
}
}

@Override
public void onMoveEnd(MoveGestureDetector detector) {
if (!interrupt && isLocationTracking()) {
moveGestureDetector.setMoveThreshold(options.trackingInitialMoveThreshold());
}
interrupt = false;
}
};

private MapboxMap.OnRotateListener onRotateListener = new MapboxMap.OnRotateListener() {
@Override
public void onRotateBegin(RotateGestureDetector detector) {
if (isBearingTracking()) {
setCameraMode(CameraMode.NONE);
}
}

@Override
public void onRotate(RotateGestureDetector detector) {
// no implementation
}

@Override
public void onRotateEnd(RotateGestureDetector detector) {
// no implementation
}
};
}
Loading

0 comments on commit c149689

Please sign in to comment.