[android] #2758 - add my location tracking mode
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.mapbox.mapboxsdk.constants;
|
||||
|
||||
import android.support.annotation.IntDef;
|
||||
|
||||
import com.mapbox.mapboxsdk.views.MapView;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* MyLocationTracking exposes different types location tracking modes.
|
||||
*
|
||||
* @see MapView#setMyLocationTrackingMode(int)
|
||||
* @see com.mapbox.mapboxsdk.views.UserLocationView#setMyLocationTrackingMode(int)
|
||||
*/
|
||||
public class MyLocationTracking {
|
||||
|
||||
/**
|
||||
* Indicates the parameter accepts one of the values from {@link MyLocationTracking}.
|
||||
*/
|
||||
@IntDef({TRACKING_NONE, TRACKING_FOLLOW, /**TRACKING_FOLLOW_BEARING_GPS, TRACKING_FOLLOW_BEARING_COMPASS, TRACKING_FOLLOW_BEARING**/})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface Mode {
|
||||
}
|
||||
|
||||
/**
|
||||
* Not tracking user
|
||||
*/
|
||||
public static final int TRACKING_NONE = 0x00000000;
|
||||
|
||||
/**
|
||||
* Tracking the location of the user
|
||||
*/
|
||||
public static final int TRACKING_FOLLOW = 0x00000004;
|
||||
|
||||
// /**
|
||||
// * Tracking the location of the user with bearing from GPS
|
||||
// */
|
||||
// public static final int TRACKING_FOLLOW_BEARING_GPS = 0x00000008;
|
||||
//
|
||||
// /**
|
||||
// * Tracking the location of the user with bearing from compass
|
||||
// */
|
||||
// public static final int TRACKING_FOLLOW_BEARING_COMPASS = 0x00000012;
|
||||
//
|
||||
// /**
|
||||
// * Tracking the location fot the user with bearing from GPS and compass
|
||||
// */
|
||||
// public static final int TRACKING_FOLLOW_BEARING = 0x00000016;
|
||||
|
||||
}
|
||||
@@ -64,6 +64,7 @@ import com.mapbox.mapboxsdk.annotations.Polyline;
|
||||
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Sprite;
|
||||
import com.mapbox.mapboxsdk.annotations.SpriteFactory;
|
||||
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
||||
import com.mapbox.mapboxsdk.constants.Style;
|
||||
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
|
||||
import com.mapbox.mapboxsdk.exceptions.SpriteBitmapChangedException;
|
||||
@@ -121,6 +122,7 @@ public final class MapView extends FrameLayout {
|
||||
private static final String STATE_STYLE_CLASSES = "styleClasses";
|
||||
private static final String STATE_DEFAULT_TRANSITION_DURATION = "defaultTransitionDuration";
|
||||
private static final String STATE_MY_LOCATION_ENABLED = "myLocationEnabled";
|
||||
private static final String STATE_MY_LOCATION_TRACKING_MODE = "myLocationTracking";
|
||||
private static final String STATE_COMPASS_ENABLED = "compassEnabled";
|
||||
private static final String STATE_COMPASS_GRAVITY = "compassGravity";
|
||||
private static final String STATE_COMPASS_MARGIN_LEFT = "compassMarginLeft";
|
||||
@@ -801,6 +803,9 @@ public final class MapView extends FrameLayout {
|
||||
, savedInstanceState.getInt(STATE_ATTRIBUTION_MARGIN_TOP)
|
||||
, savedInstanceState.getInt(STATE_ATTRIBUTION_MARGIN_RIGHT)
|
||||
, savedInstanceState.getInt(STATE_ATTRIBUTION_MARGIN_BOTTOM));
|
||||
|
||||
//noinspection ResourceType
|
||||
setMyLocationTrackingMode(savedInstanceState.getInt(STATE_MY_LOCATION_TRACKING_MODE, MyLocationTracking.TRACKING_NONE));
|
||||
}
|
||||
|
||||
// Force a check for an access token
|
||||
@@ -852,6 +857,7 @@ public final class MapView extends FrameLayout {
|
||||
outState.putStringArrayList(STATE_STYLE_CLASSES, new ArrayList<>(getStyleClasses()));
|
||||
outState.putLong(STATE_DEFAULT_TRANSITION_DURATION, mNativeMapView.getDefaultTransitionDuration());
|
||||
outState.putBoolean(STATE_MY_LOCATION_ENABLED, isMyLocationEnabled());
|
||||
outState.putInt(STATE_MY_LOCATION_TRACKING_MODE, mUserLocationView.getMyLocationTrackingMode());
|
||||
|
||||
// Compass
|
||||
LayoutParams compassParams = (LayoutParams) mCompassView.getLayoutParams();
|
||||
@@ -2493,8 +2499,13 @@ public final class MapView extends FrameLayout {
|
||||
mNativeMapView.cancelTransitions();
|
||||
|
||||
// Scale the map
|
||||
mNativeMapView.scaleBy(detector.getScaleFactor(), detector.getFocusX() / mScreenDensity, detector.getFocusY() / mScreenDensity);
|
||||
|
||||
if (mUserLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE) {
|
||||
// around gesture
|
||||
mNativeMapView.scaleBy(detector.getScaleFactor(), detector.getFocusX() / mScreenDensity, detector.getFocusY() / mScreenDensity);
|
||||
} else {
|
||||
// around center map
|
||||
mNativeMapView.scaleBy(detector.getScaleFactor(), (getWidth() / 2) / mScreenDensity, (getHeight() / 2) / mScreenDensity);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -2559,11 +2570,18 @@ public final class MapView extends FrameLayout {
|
||||
// Cancel any animation
|
||||
mNativeMapView.cancelTransitions();
|
||||
|
||||
// Rotate the map
|
||||
// Get rotate value
|
||||
double bearing = mNativeMapView.getBearing();
|
||||
bearing += detector.getRotationDegreesDelta();
|
||||
mNativeMapView.setBearing(bearing, detector.getFocusX() / mScreenDensity, detector.getFocusY() / mScreenDensity);
|
||||
|
||||
// Rotate the map
|
||||
if (mUserLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE) {
|
||||
// around gesture
|
||||
mNativeMapView.setBearing(bearing, detector.getFocusX() / mScreenDensity, detector.getFocusY() / mScreenDensity);
|
||||
} else {
|
||||
// around center map
|
||||
mNativeMapView.setBearing(bearing, (getWidth() / 2) / mScreenDensity, (getHeight() / 2) / mScreenDensity);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -3116,6 +3134,42 @@ public final class MapView extends FrameLayout {
|
||||
mUserLocationView.setOnMyLocationChangeListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current my location tracking mode.
|
||||
* Tracking my location disbales gestures, automatically moves the viewport
|
||||
* and shows the direction the user is heading.
|
||||
* See {@link MyLocationTracking} for different values.
|
||||
*
|
||||
* @param userLocationTrackingMode to be used.
|
||||
* @see MyLocationTracking
|
||||
*/
|
||||
@UiThread
|
||||
public void setMyLocationTrackingMode(@MyLocationTracking.Mode int userLocationTrackingMode) {
|
||||
mUserLocationView.setMyLocationTrackingMode(userLocationTrackingMode);
|
||||
|
||||
// Enable/disable gestures based on tracking mode
|
||||
if (userLocationTrackingMode == MyLocationTracking.TRACKING_NONE) {
|
||||
mScrollEnabled = true;
|
||||
mRotateEnabled = true;
|
||||
} else {
|
||||
mScrollEnabled = false;
|
||||
mRotateEnabled = (userLocationTrackingMode == MyLocationTracking.TRACKING_FOLLOW);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current user location tracking mode.
|
||||
* See {@link MyLocationTracking} for possible return values.
|
||||
*
|
||||
* @return the current user location tracking mode.
|
||||
* @see MyLocationTracking
|
||||
*/
|
||||
@UiThread
|
||||
@MyLocationTracking.Mode
|
||||
public int getMyLocationTrackingMode() {
|
||||
return mUserLocationView.getMyLocationTrackingMode();
|
||||
}
|
||||
|
||||
//
|
||||
// Compass
|
||||
//
|
||||
|
||||
@@ -5,7 +5,6 @@ import android.animation.ValueAnimator;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
@@ -21,17 +20,16 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapzen.android.lost.api.LocationListener;
|
||||
import com.mapzen.android.lost.api.LocationRequest;
|
||||
import com.mapzen.android.lost.api.LocationServices;
|
||||
import com.mapzen.android.lost.api.LostApiClient;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
final class UserLocationView extends View {
|
||||
|
||||
private WeakReference<MapView> mMapView;
|
||||
private MapView mMapView;
|
||||
|
||||
private static final int BLUE_COLOR = 0x39ACCBFF;
|
||||
|
||||
@@ -79,6 +77,9 @@ final class UserLocationView extends View {
|
||||
|
||||
MapView.OnMyLocationChangeListener mOnMyLocationChangeListener;
|
||||
|
||||
@MyLocationTracking.Mode
|
||||
private int mMyLocationTrackingMode;
|
||||
|
||||
public UserLocationView(MapView mapView, Context context) {
|
||||
super(context);
|
||||
initialize(mapView, context);
|
||||
@@ -95,7 +96,7 @@ final class UserLocationView extends View {
|
||||
}
|
||||
|
||||
private void initialize(MapView mapView, Context context) {
|
||||
mMapView = new WeakReference<MapView>(mapView);
|
||||
mMapView = mapView;
|
||||
|
||||
// View configuration
|
||||
setEnabled(false);
|
||||
@@ -208,6 +209,20 @@ final class UserLocationView extends View {
|
||||
}
|
||||
}
|
||||
|
||||
public void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTrackingMode) {
|
||||
mMyLocationTrackingMode = myLocationTrackingMode;
|
||||
|
||||
if (myLocationTrackingMode != MyLocationTracking.TRACKING_NONE && mUserLocation != null) {
|
||||
// center map directly if we have a location fix
|
||||
mMapView.setCenterCoordinate(new LatLng(mUserLocation.getLatitude(), mUserLocation.getLongitude()));
|
||||
}
|
||||
}
|
||||
|
||||
@MyLocationTracking.Mode
|
||||
public int getMyLocationTrackingMode() {
|
||||
return mMyLocationTrackingMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
super.setEnabled(enabled);
|
||||
@@ -221,7 +236,7 @@ final class UserLocationView extends View {
|
||||
|
||||
// compute new marker position
|
||||
// TODO add JNI method that takes existing pointf
|
||||
mMarkerScreenPoint = mMapView.get().toScreenLocation(mMarkerCoordinate);
|
||||
mMarkerScreenPoint = mMapView.toScreenLocation(mMarkerCoordinate);
|
||||
mMarkerScreenMatrix.reset();
|
||||
mMarkerScreenMatrix.setTranslate(
|
||||
mMarkerScreenPoint.x,
|
||||
@@ -230,14 +245,14 @@ final class UserLocationView extends View {
|
||||
// rotate so arrow in points to bearing
|
||||
if (mShowDirection) {
|
||||
mMarkerScreenMatrix.preRotate(mMarkerDirection +
|
||||
(float) mMapView.get().getDirection());
|
||||
(float) mMapView.getDirection());
|
||||
}
|
||||
|
||||
// adjust accuracy circle
|
||||
if (mShowAccuracy) {
|
||||
mAccuracyPath.reset();
|
||||
mAccuracyPath.addCircle(0.0f, 0.0f,
|
||||
(float) (mMarkerAccuracy / mMapView.get().getMetersPerPixelAtLatitude(
|
||||
(float) (mMarkerAccuracy / mMapView.getMetersPerPixelAtLatitude(
|
||||
mMarkerCoordinate.getLatitude())),
|
||||
Path.Direction.CW);
|
||||
|
||||
@@ -344,12 +359,19 @@ final class UserLocationView extends View {
|
||||
previousCoordinate = new LatLng(mUserLocation);
|
||||
}
|
||||
|
||||
mMarkerCoordinateAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
|
||||
mMarkerCoordinateAnimator.setDuration(1000);
|
||||
mMarkerCoordinateAnimator.addUpdateListener(new MarkerCoordinateAnimatorListener(
|
||||
previousCoordinate, new LatLng(location)
|
||||
));
|
||||
mMarkerCoordinateAnimator.start();
|
||||
if(mMyLocationTrackingMode== MyLocationTracking.TRACKING_NONE) {
|
||||
// moving marker above map
|
||||
mMarkerCoordinateAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
|
||||
mMarkerCoordinateAnimator.setDuration(1000);
|
||||
mMarkerCoordinateAnimator.addUpdateListener(new MarkerCoordinateAnimatorListener(
|
||||
previousCoordinate, new LatLng(location)
|
||||
));
|
||||
mMarkerCoordinateAnimator.start();
|
||||
}else{
|
||||
// moving map under the tracker
|
||||
mMarkerCoordinate = new LatLng(location);
|
||||
mMapView.setCenterCoordinate(mMarkerCoordinate, true);
|
||||
}
|
||||
|
||||
mShowDirection = location.hasBearing();
|
||||
if (mShowDirection) {
|
||||
@@ -388,7 +410,7 @@ final class UserLocationView extends View {
|
||||
}
|
||||
|
||||
void updateOnNextFrame() {
|
||||
mMapView.get().update();
|
||||
mMapView.update();
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
|
||||
Reference in New Issue
Block a user