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

add bearing and tilt to getCameraForLatLngBounds #12290

Merged
merged 3 commits into from
Jul 5, 2018
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 @@ -146,8 +146,8 @@ private void initialiseMap() {
nativeMapView.addOnMapChangedListener(mapCallback);

// callback for focal point invalidation
final FocalPointInvalidator focalPointInvalidator = new FocalPointInvalidator();
focalPointInvalidator.addListener(createFocalPointChangeListener());
final FocalPointInvalidator focalInvalidator = new FocalPointInvalidator();
focalInvalidator.addListener(createFocalPointChangeListener());

// callback for registering touch listeners
GesturesManagerInteractionListener registerTouchListener = new GesturesManagerInteractionListener();
Expand All @@ -157,7 +157,7 @@ private void initialiseMap() {

// setup components for MapboxMap creation
Projection proj = new Projection(nativeMapView);
UiSettings uiSettings = new UiSettings(proj, focalPointInvalidator, compassView, attrView, logoView);
UiSettings uiSettings = new UiSettings(proj, focalInvalidator, compassView, attrView, logoView, getPixelRatio());
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer));
IconManager iconManager = new IconManager(nativeMapView);
Expand Down Expand Up @@ -310,15 +310,12 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
addView(glSurfaceView, 0);
}

nativeMapView = new NativeMapView(getContext(), this, mapRenderer);
nativeMapView.addOnMapChangedListener(new OnMapChangedListener() {
@Override
public void onMapChanged(int change) {
// dispatch events to external listeners
if (!onMapChangedListeners.isEmpty()) {
for (OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
onMapChangedListener.onMapChanged(change);
}
nativeMapView = new NativeMapView(getContext(), getPixelRatio(), this, mapRenderer);
nativeMapView.addOnMapChangedListener(change -> {
// dispatch events to external listeners
if (!onMapChangedListeners.isEmpty()) {
for (OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
onMapChangedListener.onMapChanged(change);
}
}
});
Expand Down Expand Up @@ -583,6 +580,16 @@ protected void onSizeChanged(int width, int height, int oldw, int oldh) {
}
}

private float getPixelRatio() {
// check is user defined his own pixel ratio value
float pixelRatio = mapboxMapOptions.getPixelRatio();
if (pixelRatio == 0) {
// if not, get the one defined by the system
pixelRatio = getResources().getDisplayMetrics().density;
}
return pixelRatio;
}

//
// View events
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
import android.support.annotation.UiThread;
import android.support.v4.util.Pools;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.android.gestures.MoveGestureDetector;
import com.mapbox.android.gestures.RotateGestureDetector;
Expand Down Expand Up @@ -44,12 +44,11 @@
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.light.Light;
import com.mapbox.mapboxsdk.style.sources.Source;
import timber.log.Timber;

import java.util.HashMap;
import java.util.List;

import timber.log.Timber;

/**
* The general class to interact with in the Android Mapbox SDK. It exposes the entry point for all
* methods related to the MapView. You cannot instantiate {@link MapboxMap} object directly, rather,
Expand Down Expand Up @@ -457,7 +456,7 @@ public void addImage(@NonNull String name, @NonNull Bitmap image) {
*
* @param name the name of the image
* @param image the pre-multiplied Bitmap
* @param sdf the flag indicating image is an SDF or template image
* @param sdf the flag indicating image is an SDF or template image
*/
public void addImage(@NonNull String name, @NonNull Bitmap image, boolean sdf) {
nativeMapView.addImage(name, image, sdf);
Expand Down Expand Up @@ -1567,31 +1566,147 @@ public void setLatLngBoundsForCameraTarget(@Nullable LatLngBounds latLngBounds)
nativeMapView.setLatLngBounds(latLngBounds);
}


/**
* Get a camera position that fits a provided bounds and padding.
* Get a camera position that fits a provided bounds and the current camera tilt and bearing.
*
* @param latLngBounds the bounds to constrain the map with
* @param latLngBounds the bounds to set the map with
* @return the camera position that fits the bounds
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds) {
// we use current camera tilt value to provide expected transformations as #11993
return getCameraForLatLngBounds(latLngBounds, new int[] {0, 0, 0, 0});
}


/**
* Get a camera position that fits a provided bounds and padding and the current camera tilt and bearing.
*
* @param latLngBounds the bounds to set the map with
* @param padding the padding to apply to the bounds
* @return the camera position that fits the bounds and padding
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds, int[] padding) {
// get padded camera position from LatLngBounds
return nativeMapView.getCameraForLatLngBounds(latLngBounds, padding);
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
@NonNull @Size(value = 4) int[] padding) {
// we use current camera tilt/bearing value to provide expected transformations as #11993
return getCameraForLatLngBounds(latLngBounds, padding, transform.getBearing(), transform.getTilt());
}


/**
* Get a camera position that fits a provided bounds, bearing and tilt.
*
* @param latLngBounds the bounds to set the map with
* @param bearing the bearing to transform the camera position with
* @param tilt to transform the camera position with
* @return the camera position that fits the bounds and given bearing and tilt
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION,
to = MapboxConstants.MAXIMUM_DIRECTION) double bearing,
@FloatRange(from = MapboxConstants.MINIMUM_TILT,
to = MapboxConstants.MAXIMUM_TILT) double tilt) {
return getCameraForLatLngBounds(latLngBounds, new int[] {0, 0, 0, 0}, bearing, tilt);
}


/**
* Get a camera position that fits a provided bounds, padding, bearing and tilt.
*
* @param latLngBounds the bounds to set the map with
* @param padding the padding to apply to the bounds
* @param bearing the bearing to transform the camera position with
* @param tilt to transform the camera position with
* @return the camera position that fits the bounds, bearing and tilt
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
@NonNull @Size(value = 4) int[] padding,
@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION,
to = MapboxConstants.MAXIMUM_DIRECTION) double bearing,
@FloatRange(from = MapboxConstants.MINIMUM_TILT,
to = MapboxConstants.MAXIMUM_TILT) double tilt) {
return nativeMapView.getCameraForLatLngBounds(latLngBounds, padding, bearing, tilt);
}

/**
* Get a camera position that fits a provided shape.
*
* @param geometry the geometry to wraps the map with
* @return the camera position that fits the geometry inside
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry) {
// we use current camera tilt value to provide expected transformations as #11993
return getCameraForGeometry(geometry, new int[] {0, 0, 0, 0});
}

/**
* Get a camera position that fits a provided shape and padding.
*
* @param geometry the geometry to wraps the map with
* @param padding the padding to apply to the bounds
* @return the camera position that fits the geometry inside and padding
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
@NonNull @Size(value = 4) int[] padding) {
// we use current camera tilt/bearing value to provide expected transformations as #11993
return getCameraForGeometry(geometry, padding, transform.getBearing(), transform.getTilt());
}

/**
* Get a camera position that fits a provided shape with a given bearing and tilt.
*
* @param geometry the geometry to wraps the map with
* @param bearing the bearing at which to compute the geometry's bounds
* @param tilt the tilt at which to compute the geometry's bounds
* @return the camera position that the geometry inside with bearing and tilt
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION,
to = MapboxConstants.MAXIMUM_DIRECTION) double bearing,
@FloatRange(from = MapboxConstants.MINIMUM_TILT,
to = MapboxConstants.MAXIMUM_TILT) double tilt) {
return getCameraForGeometry(geometry, new int[] {0, 0, 0, 0}, bearing, tilt);
}

/**
* Get a camera position that fits a provided shape with a given padding, bearing and tilt.
*
* @param geometry the geometry to wraps the map with
* @param padding the padding to apply to the bounds
* @param bearing the bearing at which to compute the geometry's bounds
* @param tilt the tilt at which to compute the geometry's bounds
* @return the camera position that fits the geometry inside with padding, bearing and tilt
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
@NonNull @Size(value = 4) int[] padding,
@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION,
to = MapboxConstants.MAXIMUM_DIRECTION) double bearing,
@FloatRange(from = MapboxConstants.MINIMUM_TILT,
to = MapboxConstants.MAXIMUM_TILT) double tilt) {
return nativeMapView.getCameraForGeometry(geometry, padding, bearing, tilt);
}

/**
* Get a camera position that fits a provided shape with a given bearing and padding.
*
* @param geometry the geometry to constrain the map with
* @param geometry the geometry to wraps the map with
* @param bearing the bearing at which to compute the geometry's bounds
* @param padding the padding to apply to the bounds
* @return the camera position that fits the bounds and padding
* @return the camera position that fits the geometry inside with padding and bearing
* @deprecated use Mapbox{@link #getCameraForGeometry(Geometry, int[], double, double)} instead
*/
@NonNull
@Deprecated
public CameraPosition getCameraForGeometry(Geometry geometry, double bearing, int[] padding) {
// get padded camera position from Geometry
return nativeMapView.getCameraForGeometry(geometry, bearing, padding);
return getCameraForGeometry(geometry, padding, bearing, transform.getTilt());
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.view.Gravity;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
Expand Down Expand Up @@ -76,6 +75,8 @@ public class MapboxMapOptions implements Parcelable {

private String style;

private float pixelRatio;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are adding an option to set the pixel ratio, I think this value should also be pushed to the UiSettings during map initialisation.


/**
* Creates a new MapboxMapOptions object.
*/
Expand Down Expand Up @@ -122,6 +123,7 @@ private MapboxMapOptions(Parcel in) {
prefetchesTiles = in.readByte() != 0;
zMediaOverlay = in.readByte() != 0;
localIdeographFontFamily = in.readString();
pixelRatio = in.readFloat();
}

/**
Expand Down Expand Up @@ -218,6 +220,8 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableZMediaOverlay, false));
mapboxMapOptions.localIdeographFontFamily(
typedArray.getString(R.styleable.mapbox_MapView_mapbox_localIdeographFontFamily));
mapboxMapOptions.pixelRatio(
typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_pixelRatio, 0));
} finally {
typedArray.recycle();
}
Expand Down Expand Up @@ -545,6 +549,18 @@ public MapboxMapOptions localIdeographFontFamily(String fontFamily) {
return this;
}

/**
* Set the custom pixel ratio configuration to override the default value from resources.
* This ratio will be used to initialise the map with.
*
* @param pixelRatio the custom pixel ratio of the map under construction
* @return This
*/
public MapboxMapOptions pixelRatio(float pixelRatio) {
this.pixelRatio = pixelRatio;
return this;
}

/**
* Check whether tile pre-fetching is enabled.
*
Expand Down Expand Up @@ -813,6 +829,15 @@ public String getLocalIdeographFontFamily() {
return localIdeographFontFamily;
}

/**
* Return the custom configured pixel ratio, returns 0 if not configured.
*
* @return the pixel ratio used by the map under construction
*/
public float getPixelRatio() {
return pixelRatio;
}

public static final Parcelable.Creator<MapboxMapOptions> CREATOR = new Parcelable.Creator<MapboxMapOptions>() {
public MapboxMapOptions createFromParcel(Parcel in) {
return new MapboxMapOptions(in);
Expand Down Expand Up @@ -866,6 +891,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (prefetchesTiles ? 1 : 0));
dest.writeByte((byte) (zMediaOverlay ? 1 : 0));
dest.writeString(localIdeographFontFamily);
dest.writeFloat(pixelRatio);
}

@Override
Expand Down Expand Up @@ -959,7 +985,10 @@ public boolean equals(Object o) {
if (zMediaOverlay != options.zMediaOverlay) {
return false;
}
if (localIdeographFontFamily != options.localIdeographFontFamily) {
if (!localIdeographFontFamily.equals(options.localIdeographFontFamily)) {
return false;
}
if (pixelRatio != options.pixelRatio) {
return false;
}

Expand Down Expand Up @@ -1001,6 +1030,7 @@ public int hashCode() {
result = 31 * result + (prefetchesTiles ? 1 : 0);
result = 31 * result + (zMediaOverlay ? 1 : 0);
result = 31 * result + (localIdeographFontFamily != null ? localIdeographFontFamily.hashCode() : 0);
result = 31 * result + (int) pixelRatio;
return result;
}
}
Loading