Skip to content

Commit 11f883a

Browse files
committed
feat: animateCamera supports bounds and zoomLevel on both platforms
1 parent e17b5a8 commit 11f883a

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

src/mapbox.android.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -1754,32 +1754,39 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
17541754
animateCamera(options: AnimateCameraOptions, nativeMap?): Promise<void> {
17551755
return new Promise((resolve, reject) => {
17561756
try {
1757-
const target = options.target;
1758-
if (target === undefined) {
1759-
reject("Please set the 'target' parameter");
1760-
return;
1761-
}
1757+
const durationMs = options.duration ? options.duration : 10000;
1758+
if (options.bounds) {
1759+
const padding = options.padding || 0;
1760+
const bounds = new com.mapbox.mapboxsdk.geometry.LatLngBounds.Builder()
1761+
.include(new com.mapbox.mapboxsdk.geometry.LatLng(options.bounds.north, options.bounds.east))
1762+
.include(new com.mapbox.mapboxsdk.geometry.LatLng(options.bounds.south, options.bounds.west))
1763+
.build();
1764+
this._mapboxMapInstance.animateCamera(com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newLatLngBounds(bounds, padding), durationMs, null);
1765+
} else {
1766+
const target = options.target;
1767+
if (target === undefined) {
1768+
reject("Please set the 'target' parameter");
1769+
return;
1770+
}
17621771

1763-
const cameraPositionBuilder = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder(this._mapboxMapInstance.getCameraPosition()).target(
1764-
new com.mapbox.mapboxsdk.geometry.LatLng(target.lat, target.lng)
1765-
);
1772+
const cameraPositionBuilder = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder(this._mapboxMapInstance.getCameraPosition()).target(
1773+
new com.mapbox.mapboxsdk.geometry.LatLng(target.lat, target.lng)
1774+
);
17661775

1767-
if (options.bearing) {
1768-
cameraPositionBuilder.bearing(options.bearing);
1769-
}
1776+
if (options.bearing) {
1777+
cameraPositionBuilder.bearing(options.bearing);
1778+
}
17701779

1771-
if (options.tilt) {
1772-
cameraPositionBuilder.tilt(options.tilt);
1773-
}
1780+
if (options.tilt) {
1781+
cameraPositionBuilder.tilt(options.tilt);
1782+
}
17741783

1775-
if (options.zoomLevel) {
1776-
cameraPositionBuilder.zoom(options.zoomLevel);
1784+
if (options.zoomLevel) {
1785+
cameraPositionBuilder.zoom(options.zoomLevel);
1786+
}
1787+
this._mapboxMapInstance.animateCamera(com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPositionBuilder.build()), durationMs, null);
17771788
}
17781789

1779-
const durationMs = options.duration ? options.duration : 10000;
1780-
1781-
this._mapboxMapInstance.animateCamera(com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPositionBuilder.build()), durationMs, null);
1782-
17831790
setTimeout(() => {
17841791
resolve();
17851792
}, durationMs);

src/mapbox.common.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,9 @@ export interface ShowResult {
511511
// ------------------------------------------------------------
512512

513513
export interface AnimateCameraOptions {
514-
target: LatLng;
515-
/**
516-
* For Android, 0.0 - 20.0
517-
*/
514+
target?: LatLng;
515+
bounds?: Bounds;
516+
padding?: number;
518517
zoomLevel?: number;
519518
/**
520519
* For iOS, in meters from the ground

src/mapbox.ios.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -1634,16 +1634,31 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
16341634
try {
16351635
const theMap: MGLMapView = nativeMap || this._mapboxViewInstance;
16361636

1637-
const target = options.target;
1638-
if (target === undefined) {
1639-
reject("Please set the 'target' parameter");
1640-
return;
1637+
let cam: MGLMapCamera;
1638+
if (options.bounds) {
1639+
const padding = options.padding || 0;
1640+
// support defined padding
1641+
const insets: UIEdgeInsets = {
1642+
top: padding,
1643+
left: padding,
1644+
bottom: padding,
1645+
right: padding
1646+
};
1647+
const bounds: MGLCoordinateBounds = {
1648+
sw: CLLocationCoordinate2DMake(options.bounds.south, options.bounds.west),
1649+
ne: CLLocationCoordinate2DMake(options.bounds.north, options.bounds.east)
1650+
};
1651+
cam = theMap.cameraThatFitsCoordinateBoundsEdgePadding(bounds, insets);
1652+
} else {
1653+
const target = options.target;
1654+
if (target === undefined) {
1655+
reject("Please set the 'target' parameter");
1656+
return;
1657+
}
1658+
cam = theMap.camera;
1659+
cam.centerCoordinate = CLLocationCoordinate2DMake(target.lat, target.lng);
16411660
}
16421661

1643-
const cam = theMap.camera;
1644-
1645-
cam.centerCoordinate = CLLocationCoordinate2DMake(target.lat, target.lng);
1646-
16471662
if (options.altitude) {
16481663
cam.altitude = options.altitude;
16491664
}
@@ -1655,6 +1670,9 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
16551670
if (options.tilt) {
16561671
cam.pitch = options.tilt;
16571672
}
1673+
if (options.zoomLevel && options.target) {
1674+
cam.altitude = MGLAltitudeForZoomLevel(options.zoomLevel, cam.pitch, options.target.lat, theMap.frame.size);
1675+
}
16581676

16591677
const durationMs = options.duration ? options.duration : 10000;
16601678

0 commit comments

Comments
 (0)