Skip to content

Commit

Permalink
feat(web): add missing setCamera method to Camera component (#3581)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertlaiuste committed Aug 10, 2024
1 parent cdce1cd commit 7f702dc
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/web/components/Camera.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, ContextType } from 'react';

import { CameraProps, CameraRef } from '../../components/Camera';
import { CameraProps, CameraStop, CameraRef } from '../../components/Camera';
import { Position } from '../../types/Position';
import MapContext from '../MapContext';

Expand Down Expand Up @@ -140,6 +140,66 @@ class Camera
}
}

setCamera(props: CameraStop) {
const { map } = this.context;
if (!map) {
return;
}
const {
centerCoordinate,
bounds,
zoomLevel,
heading,
pitch,
padding,
animationDuration = 2000,
} = props;

let options: mapboxgl.CameraOptions = {
center: centerCoordinate?.slice(0, 2) as [number, number],
zoom: zoomLevel ?? map.getZoom(),
bearing: heading ?? map.getBearing(),
pitch: pitch ?? map.getPitch(),
};

if (
padding?.paddingTop &&
padding?.paddingRight &&
padding?.paddingBottom &&
padding?.paddingLeft
) {
options.padding = buildMapboxGlPadding([
padding.paddingTop,
padding.paddingRight,
padding.paddingBottom,
padding.paddingLeft,
]);
}

if (bounds?.ne && bounds?.sw) {
const newCameraTransform = map.cameraForBounds(
[bounds.ne as mapboxgl.LngLatLike, bounds.sw as mapboxgl.LngLatLike],
options,
);
options = { ...options, ...newCameraTransform };
}

switch (props.animationMode) {
default:
case 'easeTo':
case 'linearTo':
map.easeTo({ ...options, duration: animationDuration });
break;
case 'flyTo':
map.flyTo({ ...options, duration: animationDuration });
break;
case 'moveTo':
case 'none':
map.jumpTo(options);
break;
}
}

render() {
return <></>;
}
Expand Down

0 comments on commit 7f702dc

Please sign in to comment.