Compass click rotates around focal point (if available) (#7977)
* add set bearing method taking focal point into account * notify CompassView when FocalPoint changes
This commit is contained in:
@@ -127,7 +127,7 @@ public class MapView extends FrameLayout {
|
||||
nativeMapView = new NativeMapView(this);
|
||||
|
||||
// callback for focal point invalidation
|
||||
FocalPointInvalidator focalPoint = new FocalPointInvalidator();
|
||||
FocalPointInvalidator focalPoint = new FocalPointInvalidator(compassView);
|
||||
|
||||
// callback for registering touch listeners
|
||||
RegisterTouchListener registerTouchListener = new RegisterTouchListener();
|
||||
@@ -944,9 +944,18 @@ public class MapView extends FrameLayout {
|
||||
|
||||
private class FocalPointInvalidator implements FocalPointChangeListener {
|
||||
|
||||
private final FocalPointChangeListener[] focalPointChangeListeners;
|
||||
|
||||
FocalPointInvalidator(FocalPointChangeListener... listeners) {
|
||||
focalPointChangeListeners = listeners;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFocalPointChanged(PointF pointF) {
|
||||
mapGestureDetector.setFocalPoint(pointF);
|
||||
for (FocalPointChangeListener focalPointChangeListener : focalPointChangeListeners) {
|
||||
focalPointChangeListener.onFocalPointChanged(pointF);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -738,6 +738,21 @@ public final class MapboxMap {
|
||||
transform.resetNorth();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set focal bearing.
|
||||
*/
|
||||
public void setFocalBearing(double bearing, float focalX, float focalY, long duration) {
|
||||
transform.setBearing(bearing, focalX, focalY, duration);
|
||||
}
|
||||
|
||||
public float getHeight() {
|
||||
return nativeMapView.getHeight();
|
||||
}
|
||||
|
||||
public float getWidth() {
|
||||
return nativeMapView.getWidth();
|
||||
}
|
||||
|
||||
//
|
||||
// Debug
|
||||
//
|
||||
|
||||
@@ -530,6 +530,13 @@ final class NativeMapView {
|
||||
nativeSetBearingXY(nativeMapViewPtr, degrees, cx / pixelRatio, cy / pixelRatio);
|
||||
}
|
||||
|
||||
public void setBearing(double degrees, double fx, double fy, long duration) {
|
||||
if (isDestroyedOn("setBearing")) {
|
||||
return;
|
||||
}
|
||||
nativeSetFocalBearing(nativeMapViewPtr, degrees, fx / pixelRatio, fy / pixelRatio, duration);
|
||||
}
|
||||
|
||||
public double getBearing() {
|
||||
if (isDestroyedOn("getBearing")) {
|
||||
return 0;
|
||||
@@ -1047,6 +1054,9 @@ final class NativeMapView {
|
||||
private native void nativeSetBearingXY(long nativeMapViewPtr, double degrees,
|
||||
double cx, double cy);
|
||||
|
||||
private native void nativeSetFocalBearing(long nativeMapViewPtr, double degrees,
|
||||
double fx, double fy, long duration);
|
||||
|
||||
private native double nativeGetBearing(long nativeMapViewPtr);
|
||||
|
||||
private native void nativeResetNorth(long nativeMapViewPtr);
|
||||
|
||||
@@ -220,6 +220,13 @@ final class Transform implements MapView.OnMapChangedListener {
|
||||
mapView.setBearing(bearing, focalX, focalY);
|
||||
}
|
||||
|
||||
void setBearing(double bearing, float focalX, float focalY, long duration) {
|
||||
if (myLocationView != null) {
|
||||
myLocationView.setBearing(bearing);
|
||||
}
|
||||
mapView.setBearing(bearing, focalX, focalY, duration);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// LatLng / CenterCoordinate
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
package com.mapbox.mapboxsdk.maps.widgets;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.PointF;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.view.ViewCompat;
|
||||
import android.support.v4.view.ViewPropertyAnimatorCompat;
|
||||
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
|
||||
import android.support.v7.widget.AppCompatImageView;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.maps.FocalPointChangeListener;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
@@ -25,7 +28,7 @@ import java.lang.ref.WeakReference;
|
||||
* use {@link com.mapbox.mapboxsdk.maps.UiSettings}.
|
||||
* </p>
|
||||
*/
|
||||
public final class CompassView extends ImageView implements Runnable {
|
||||
public final class CompassView extends AppCompatImageView implements Runnable, FocalPointChangeListener {
|
||||
|
||||
private static final long TIME_WAIT_IDLE = 500;
|
||||
private static final long TIME_FADE_ANIMATION = TIME_WAIT_IDLE;
|
||||
@@ -34,6 +37,7 @@ public final class CompassView extends ImageView implements Runnable {
|
||||
private double direction = 0.0;
|
||||
private boolean fadeCompassViewFacingNorth = true;
|
||||
private ViewPropertyAnimatorCompat fadeAnimator;
|
||||
private PointF focalPoint;
|
||||
|
||||
public CompassView(Context context) {
|
||||
super(context);
|
||||
@@ -96,6 +100,11 @@ public final class CompassView extends ImageView implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
PointF getFocalPoint() {
|
||||
return focalPoint;
|
||||
}
|
||||
|
||||
public void update(final double direction) {
|
||||
this.direction = direction;
|
||||
|
||||
@@ -143,6 +152,11 @@ public final class CompassView extends ImageView implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFocalPointChanged(PointF pointF) {
|
||||
focalPoint = pointF;
|
||||
}
|
||||
|
||||
static class CompassClickListener implements View.OnClickListener {
|
||||
|
||||
private WeakReference<MapboxMap> mapboxMap;
|
||||
@@ -158,7 +172,12 @@ public final class CompassView extends ImageView implements Runnable {
|
||||
final MapboxMap mapboxMap = this.mapboxMap.get();
|
||||
final CompassView compassView = this.compassView.get();
|
||||
if (mapboxMap != null && compassView != null) {
|
||||
mapboxMap.resetNorth();
|
||||
PointF focalPoint = compassView.getFocalPoint();
|
||||
if (focalPoint != null) {
|
||||
mapboxMap.setFocalBearing(0, focalPoint.x, focalPoint.y, TIME_MAP_NORTH_ANIMATION);
|
||||
} else {
|
||||
mapboxMap.setFocalBearing(0, mapboxMap.getWidth() / 2, mapboxMap.getHeight() / 2, TIME_MAP_NORTH_ANIMATION);
|
||||
}
|
||||
compassView.postDelayed(compassView, TIME_WAIT_IDLE + TIME_MAP_NORTH_ANIMATION);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -618,6 +618,14 @@ void nativeSetBearing(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jd
|
||||
nativeMapView->getMap().setBearing(degrees, duration);
|
||||
}
|
||||
|
||||
void nativeSetFocalBearing(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble degrees, jdouble fx,
|
||||
jdouble fy, jlong milliseconds) {
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
mbgl::ScreenCoordinate center(fx, fy);
|
||||
nativeMapView->getMap().setBearing(degrees, center, mbgl::Milliseconds(milliseconds));
|
||||
}
|
||||
|
||||
void nativeSetBearingXY(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble degrees,
|
||||
jdouble cx, jdouble cy) {
|
||||
assert(nativeMapViewPtr != 0);
|
||||
@@ -1894,6 +1902,7 @@ void registerNatives(JavaVM *vm) {
|
||||
MAKE_NATIVE_METHOD(nativeSetMaxZoom, "(JD)V"),
|
||||
MAKE_NATIVE_METHOD(nativeRotateBy, "(JDDDDJ)V"),
|
||||
MAKE_NATIVE_METHOD(nativeSetBearing, "(JDJ)V"),
|
||||
MAKE_NATIVE_METHOD(nativeSetFocalBearing, "(JDDDJ)V"),
|
||||
MAKE_NATIVE_METHOD(nativeSetBearingXY, "(JDDD)V"),
|
||||
MAKE_NATIVE_METHOD(nativeGetBearing, "(J)D"),
|
||||
MAKE_NATIVE_METHOD(nativeResetNorth, "(J)V"),
|
||||
|
||||
Reference in New Issue
Block a user