Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

4924 make gesture focal point configurable #5332

Merged
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 @@ -152,6 +152,8 @@ public class MapView extends FrameLayout {
private int mContentPaddingRight;
private int mContentPaddingBottom;

private PointF mFocalPoint;

private StyleInitializer mStyleInitializer;

private List<OnMapReadyCallback> mOnMapReadyCallbackList;
Expand Down Expand Up @@ -566,6 +568,18 @@ public void onResume() {
}
}

void setFocalPoint(PointF focalPoint) {
if (focalPoint == null) {
// resetting focal point,
UiSettings uiSettings = mMapboxMap.getUiSettings();
// need to validate if we need to reset focal point with user provided one
if (uiSettings.getFocalPoint() != null) {
focalPoint = uiSettings.getFocalPoint();
}
}
mFocalPoint = focalPoint;
}

/**
* You must call this method from the parent's {@link Activity#onLowMemory()} or {@link Fragment#onLowMemory()}.
*/
Expand Down Expand Up @@ -1548,8 +1562,12 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
|| mShoveGestureDetector.isInProgress();

if (mTwoTap && isTap && !inProgress) {
PointF focalPoint = TwoFingerGestureDetector.determineFocalPoint(event);
zoom(false, focalPoint.x, focalPoint.y);
if (mFocalPoint != null) {
zoom(false, mFocalPoint.x / mScreenDensity, mFocalPoint.y / mScreenDensity);
} else {
PointF focalPoint = TwoFingerGestureDetector.determineFocalPoint(event);
zoom(false, focalPoint.x, focalPoint.y);
}
mTwoTap = false;
return true;
}
Expand Down Expand Up @@ -1608,12 +1626,12 @@ public boolean onDoubleTapEvent(MotionEvent e) {
}

// Single finger double tap
if (mMapboxMap.getTrackingSettings().isLocationTrackingDisabled()) {
if (mFocalPoint != null) {
// User provided focal point
zoom(true, mFocalPoint.x, mFocalPoint.y);
} else {
// Zoom in on gesture
zoom(true, e.getX(), e.getY());
} else {
// Zoom in on user location view
zoom(true, mMyLocationView.getCenterX(), mMyLocationView.getCenterY());
}
break;
}
Expand Down Expand Up @@ -1839,23 +1857,18 @@ public boolean onScale(ScaleGestureDetector detector) {
// Gesture is a quickzoom if there aren't two fingers
mQuickZoom = !mTwoTap;

TrackingSettings trackingSettings = mMapboxMap.getTrackingSettings();

// Scale the map
if (uiSettings.isScrollGesturesEnabled() && !mQuickZoom && trackingSettings.isLocationTrackingDisabled()) {
if (mFocalPoint != null) {
// arround user provided focal point
mNativeMapView.scaleBy(detector.getScaleFactor(), mFocalPoint.x / mScreenDensity, mFocalPoint.y / mScreenDensity);
} else if (mQuickZoom) {
// around center map
mNativeMapView.scaleBy(detector.getScaleFactor(), (getWidth() / 2) / mScreenDensity, (getHeight() / 2) / mScreenDensity);
} else {
// around gesture
mNativeMapView.scaleBy(detector.getScaleFactor(), detector.getFocusX() / mScreenDensity, detector.getFocusY() / mScreenDensity);
} else {
if (trackingSettings.isLocationTrackingDisabled()) {
// around center map
mNativeMapView.scaleBy(detector.getScaleFactor(), (getWidth() / 2) / mScreenDensity, (getHeight() / 2) / mScreenDensity);
} else {
// around user location view
float x = mMyLocationView.getX() + mMyLocationView.getWidth() / 2;
float y = mMyLocationView.getY() + mMyLocationView.getHeight() / 2;
mNativeMapView.scaleBy(detector.getScaleFactor(), x / mScreenDensity, y / mScreenDensity);
}
}

return true;
}
}
Expand Down Expand Up @@ -1925,16 +1938,14 @@ public boolean onRotate(RotateGestureDetector detector) {
bearing += detector.getRotationDegreesDelta();

// Rotate the map
if (mMapboxMap.getTrackingSettings().isLocationTrackingDisabled()) {
if (mFocalPoint != null) {
// User provided focal point
mNativeMapView.setBearing(bearing, mFocalPoint.x / mScreenDensity, mFocalPoint.y / mScreenDensity);
} else {
// around gesture
mNativeMapView.setBearing(bearing,
detector.getFocusX() / mScreenDensity,
detector.getFocusY() / mScreenDensity);
} else {
// around center userlocation
float x = mMyLocationView.getX() + mMyLocationView.getWidth() / 2;
float y = mMyLocationView.getY() + mMyLocationView.getHeight() / 2;
mNativeMapView.setBearing(bearing, x / mScreenDensity, y / mScreenDensity);
}
return true;
}
Expand Down Expand Up @@ -2426,6 +2437,13 @@ void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTrackingMo
mMapboxMap.setMyLocationEnabled(true);
}
mMyLocationView.setMyLocationTrackingMode(myLocationTrackingMode);

if (myLocationTrackingMode == MyLocationTracking.TRACKING_FOLLOW) {
setFocalPoint(new PointF(mMyLocationView.getCenterX(), mMyLocationView.getCenterY()));
} else {
setFocalPoint(null);
}

MapboxMap.OnMyLocationTrackingModeChangeListener listener = mMapboxMap.getOnMyLocationTrackingModeChangeListener();
if (listener != null) {
listener.onMyLocationTrackingModeChange(myLocationTrackingMode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mapbox.mapboxsdk.maps;

import android.graphics.PointF;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.view.Gravity;
import android.view.View;
Expand Down Expand Up @@ -35,6 +37,8 @@ public class UiSettings {

private boolean deselectMarkersOnTap = true;

private PointF focalPoint;

UiSettings(@NonNull MapView mapView) {
this.mapView = mapView;
this.compassSettings = new ViewSettings();
Expand Down Expand Up @@ -563,6 +567,25 @@ public void setAllGesturesEnabled(boolean enabled) {
setZoomGesturesEnabled(enabled);
}

/**
* Sets the focal point used as center for a gesture
*
* @param focalPoint the focal point to be used.
*/
public void setFocalPoint(@Nullable PointF focalPoint) {
this.focalPoint = focalPoint;
mapView.setFocalPoint(focalPoint);
}

/**
* Returns the gesture focal point
*
* @return The focal point
*/
public PointF getFocalPoint() {
return focalPoint;
}

/**
* Returns the measured height of the MapView
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -161,6 +162,8 @@ public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNul
.icon(IconFactory.getInstance(mMapView.getContext())
.fromResource(R.drawable.ic_arsenal))
);

mMapboxMap.getUiSettings().setFocalPoint(new PointF(mMapView.getMeasuredWidth() / 2, mMapView.getMeasuredHeight() / 4));
}
});
}
Expand Down