Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
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 @@ -47,6 +47,9 @@

import timber.log.Timber;

import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_MAP_NORTH_ANIMATION;
import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE;

/**
* <p>
* A {@code MapView} provides an embeddable map interface.
Expand All @@ -73,6 +76,7 @@ public class MapView extends FrameLayout {

private MyLocationView myLocationView;
private CompassView compassView;
private PointF focalPoint;
private ImageView attrView;
private ImageView logoView;

Expand Down Expand Up @@ -143,7 +147,7 @@ private void initialiseMap() {
addOnMapChangedListener(mapCallback);

// callback for focal point invalidation
FocalPointInvalidator focalPoint = new FocalPointInvalidator(compassView);
final FocalPointInvalidator focalPointInvalidator = new FocalPointInvalidator(createFocalPointChangeListener());

// callback for registering touch listeners
RegisterTouchListener registerTouchListener = new RegisterTouchListener();
Expand All @@ -152,13 +156,15 @@ private void initialiseMap() {
CameraZoomInvalidator zoomInvalidator = new CameraZoomInvalidator();

// callback for camera change events
CameraChangeDispatcher cameraChangeDispatcher = new CameraChangeDispatcher();
final CameraChangeDispatcher cameraChangeDispatcher = new CameraChangeDispatcher();

// setup components for MapboxMap creation
Projection proj = new Projection(nativeMapView);
UiSettings uiSettings = new UiSettings(proj, focalPoint, compassView, attrView, logoView);
TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPoint, zoomInvalidator);
MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, proj, focalPoint);
UiSettings uiSettings = new UiSettings(proj, focalPointInvalidator, compassView, attrView, logoView);
TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPointInvalidator,
zoomInvalidator);
MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, proj,
focalPointInvalidator);
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer));
IconManager iconManager = new IconManager(nativeMapView);
Expand All @@ -182,8 +188,9 @@ private void initialiseMap() {
MapZoomControllerListener zoomListener = new MapZoomControllerListener(mapGestureDetector, uiSettings, transform);
mapZoomButtonController.bind(uiSettings, zoomListener);

compassView.injectCompassAnimationListener(createCompassAnimationListener(cameraChangeDispatcher));
compassView.setOnClickListener(createCompassClickListener(cameraChangeDispatcher));
// inject widgets with MapboxMap
compassView.setMapboxMap(mapboxMap);
myLocationView.setMapboxMap(mapboxMap);
attrView.setOnClickListener(new AttributionDialogManager(context, mapboxMap));

Expand All @@ -205,6 +212,49 @@ private void initialiseMap() {
}
}

private FocalPointChangeListener createFocalPointChangeListener() {
return new FocalPointChangeListener() {
@Override
public void onFocalPointChanged(PointF pointF) {
focalPoint = pointF;
}
};
}

private MapboxMap.OnCompassAnimationListener createCompassAnimationListener(final CameraChangeDispatcher
cameraChangeDispatcher) {
return new MapboxMap.OnCompassAnimationListener() {
@Override
public void onCompassAnimation() {
cameraChangeDispatcher.onCameraMove();
}

@Override
public void onCompassAnimationFinished() {
compassView.isAnimating(false);
cameraChangeDispatcher.onCameraIdle();
}
};
}

private OnClickListener createCompassClickListener(final CameraChangeDispatcher cameraChangeDispatcher) {
return new OnClickListener() {
@Override
public void onClick(View v) {
if (mapboxMap != null && compassView != null) {
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);
}
cameraChangeDispatcher.onCameraMoveStarted(MapboxMap.OnCameraMoveStartedListener.REASON_API_ANIMATION);
compassView.isAnimating(true);
compassView.postDelayed(compassView, TIME_WAIT_IDLE + TIME_MAP_NORTH_ANIMATION);
}
}
};
}

//
// Lifecycle events
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,9 @@ public double getMinZoomLevel() {
* Sets the maximum zoom level the map can be displayed at.
* </p>
* <p>
* The default maximum zoomn level is 22. The upper bound for this value is 25.5.
* The default maximum zoomn level is 22. The upper bound for this value is 25.5.
* </p>
*
* @param maxZoom The new maximum zoom level.
*/
public void setMaxZoomPreference(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM,
Expand Down Expand Up @@ -2081,6 +2082,21 @@ public interface OnCameraIdleListener {
void onCameraIdle();
}

/**
* Interface definition for a callback to be invoked for when the compass is animating.
*/
public interface OnCompassAnimationListener {
/**
* Called repeatedly as the compass continues to move after clicking on it.
*/
void onCompassAnimation();

/**
* Called when compass animation has ended.
*/
void onCompassAnimationFinished();
}

/**
* Interface definition for a callback to be invoked when a frame is rendered to the map view.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.mapbox.mapboxsdk.maps.widgets;

import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
Expand All @@ -13,11 +11,8 @@
import android.view.View;
import android.view.ViewGroup;

import com.mapbox.mapboxsdk.maps.FocalPointChangeListener;
import com.mapbox.mapboxsdk.maps.MapboxMap;

import java.lang.ref.WeakReference;

/**
* UI element overlaid on a map to show the map's bearing when it isn't true north (0.0). Tapping
* the compass resets the bearing to true north and hides the compass.
Expand All @@ -27,16 +22,17 @@
* use {@link com.mapbox.mapboxsdk.maps.UiSettings}.
* </p>
*/
public final class CompassView extends AppCompatImageView implements Runnable, FocalPointChangeListener {
public final class CompassView extends AppCompatImageView implements Runnable {

private static final long TIME_WAIT_IDLE = 500;
public static final long TIME_WAIT_IDLE = 500;
public static final long TIME_MAP_NORTH_ANIMATION = 150;
private static final long TIME_FADE_ANIMATION = TIME_WAIT_IDLE;
private static final long TIME_MAP_NORTH_ANIMATION = 150;

private float rotation = 0.0f;
private boolean fadeCompassViewFacingNorth = true;
private ViewPropertyAnimatorCompat fadeAnimator;
private PointF focalPoint;
private MapboxMap.OnCompassAnimationListener compassAnimationListener;
private boolean isAnimating = false;

public CompassView(Context context) {
super(context);
Expand All @@ -62,9 +58,12 @@ private void initialize(Context context) {
setLayoutParams(lp);
}

// TODO refactor MapboxMap and replace with interface
public void setMapboxMap(@NonNull MapboxMap mapboxMap) {
setOnClickListener(new CompassClickListener(mapboxMap, this));
public void injectCompassAnimationListener(@NonNull MapboxMap.OnCompassAnimationListener compassAnimationListener) {
this.compassAnimationListener = compassAnimationListener;
}

public void isAnimating(boolean isAnimating) {
this.isAnimating = isAnimating;
}

private void resetAnimation() {
Expand Down Expand Up @@ -97,11 +96,6 @@ public void setEnabled(boolean enabled) {
}
}

@Nullable
PointF getFocalPoint() {
return focalPoint;
}

/**
* Updates the direction of the compass.
*
Expand All @@ -126,6 +120,7 @@ public void update(final double bearing) {
setVisibility(View.VISIBLE);
}

notifyCompassAnimationListenerWhenAnimating();
setRotation(rotation);
}

Expand Down Expand Up @@ -157,7 +152,8 @@ public Drawable getCompassImage() {

@Override
public void run() {
if (isFacingNorth() && fadeCompassViewFacingNorth) {
if (isHidden()) {
compassAnimationListener.onCompassAnimationFinished();
resetAnimation();
setLayerType(View.LAYER_TYPE_HARDWARE, null);
fadeAnimator = ViewCompat.animate(CompassView.this).alpha(0.0f).setDuration(TIME_FADE_ANIMATION);
Expand All @@ -172,34 +168,9 @@ public void onAnimationEnd(View view) {
}
}

@Override
public void onFocalPointChanged(PointF pointF) {
focalPoint = pointF;
}

static class CompassClickListener implements View.OnClickListener {

private WeakReference<MapboxMap> mapboxMap;
private WeakReference<CompassView> compassView;

CompassClickListener(final MapboxMap mapboxMap, CompassView compassView) {
this.mapboxMap = new WeakReference<>(mapboxMap);
this.compassView = new WeakReference<>(compassView);
}

@Override
public void onClick(View view) {
final MapboxMap mapboxMap = this.mapboxMap.get();
final CompassView compassView = this.compassView.get();
if (mapboxMap != null && compassView != null) {
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);
}
private void notifyCompassAnimationListenerWhenAnimating() {
if (isAnimating) {
compassAnimationListener.onCompassAnimation();
}
}
}