Class CameraAbstract
Hierarchy (View Summary)
- Evented
- Camera
Index
Constructors
Properties
Methods
Constructors
constructor
- new Camera(
transform: ITransform,
cameraHelper: ICameraHelper,
options: { bearingSnap: number; zoomSnap: number },
): CameraParameters
- transform: ITransform
- cameraHelper: ICameraHelper
- options: { bearingSnap: number; zoomSnap: number }
Returns Camera
Properties
cameraHelper
handlers
terrain
transform
transformCameraUpdate
A callback used to defer camera updates or apply arbitrary constraints. If specified, this Camera instance can be used as a stateless component in React etc.
Methods
calculateCameraOptionsFromCameraLngLatAltRotation
- calculateCameraOptionsFromCameraLngLatAltRotation(
cameraLngLat: LngLatLike,
cameraAlt: number,
bearing: number,
pitch: number,
roll?: number,
): CameraOptionsGiven a camera position and rotation, calculates zoom and center point and returns them as CameraOptions.
Parameters
- cameraLngLat: LngLatLike
The lng, lat of the camera to look from
- cameraAlt: number
The altitude of the camera to look from, in meters above sea level
- bearing: number
Bearing of the camera, in degrees
- pitch: number
Pitch of the camera, in degrees
Optionalroll: numberRoll of the camera, in degrees
Returns CameraOptions
the calculated camera options
Example
// Calculate options to look from camera position(1°, 0°, 1000m) with bearing = 90°, pitch = 30°, and roll = 45° const cameraLngLat = new LngLat(1, 0); const cameraAltitude = 1000; const bearing = 90; const pitch = 30; const roll = 45; const cameraOptions = map.calculateCameraOptionsFromCameraLngLatAltRotation(cameraLngLat, cameraAltitude, bearing, pitch, roll); // Apply calculated options map.jumpTo(cameraOptions); - cameraLngLat: LngLatLike
calculateCameraOptionsFromTo
- calculateCameraOptionsFromTo(
from: LngLatLike,
altitudeFrom: number,
to: LngLatLike,
altitudeTo?: number,
): CameraOptionsGiven a camera 'from' position and a position to look at (
to), calculates zoom and camera rotation and returns them as CameraOptions.Parameters
- from: LngLatLike
The camera to look from
- altitudeFrom: number
The altitude of the camera to look from
- to: LngLatLike
The center to look at
OptionalaltitudeTo: numberOptional altitude of the center to look at. If none given the ground height will be used.
Returns CameraOptions
the calculated camera options
Example
// Calculate options to look from (1°, 0°, 1000m) to (1°, 1°, 0m) const cameraLngLat = new LngLat(1, 0); const cameraAltitude = 1000; const targetLngLat = new LngLat(1, 1); const targetAltitude = 0; const cameraOptions = map.calculateCameraOptionsFromTo(cameraLngLat, cameraAltitude, targetLngLat, targetAltitude); // Apply calculated options map.jumpTo(cameraOptions); - from: LngLatLike
cameraForBounds
- cameraForBounds(
bounds: LngLatBoundsLike,
options?: CameraForBoundsOptions,
): CenterZoomBearing | undefinedParameters
- bounds: LngLatBoundsLike
Calculate the center for these bounds in the viewport and use the highest zoom level up to and including Map.getMaxZoom that fits in the viewport. LngLatBounds represent a box that is always axis-aligned with bearing 0. Bounds will be taken in [sw, ne] order. Southwest point will always be to the left of the northeast point.
Optionaloptions: CameraForBoundsOptionsOptions object
Returns CenterZoomBearing | undefined
If map is able to fit to provided bounds, returns
center,zoom, andbearing. If map is unable to fit, method will warn and return undefined. - bounds: LngLatBoundsLike
easeTo
Changes any combination of
center,zoom,bearing,pitch,roll, andpaddingwith an animated transition between old and new values. The map will retain its current values for any details not specified inoptions.!!! note "Reduced Motion" The transition will happen instantly if the user has enabled the
reduced motionaccessibility feature enabled in their operating system, unlessoptionsincludesessential: true.Triggers the following events:
movestart,move,moveend,zoomstart,zoom,zoomend,pitchstart,pitch,pitchend,rollstart,roll,rollend, androtate.Parameters
- options: EaseToOptions
Options describing the destination and animation of the transition. Accepts CameraOptions and AnimationOptions.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- options: EaseToOptions
fire
Parameters
- event: string | Event
Optionalproperties: any
Returns this
fitBounds
Pans and zooms the map to contain its visible area within the specified geographical bounds. This function will also reset the map's bearing to 0 if bearing is nonzero.
Triggers the following events:
movestartandmoveend.Parameters
- bounds: LngLatBoundsLike
Center these bounds in the viewport and use the highest zoom level up to and including Map.getMaxZoom that fits them in the viewport. Bounds will be taken in [sw, ne] order. Southwest point will always be to the left of the northeast point.
Optionaloptions: FitBoundsOptionsOptions supports all properties from AnimationOptions and CameraOptions in addition to the fields below.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- bounds: LngLatBoundsLike
fitScreenCoordinates
- fitScreenCoordinates(
p0: PointLike,
p1: PointLike,
bearing: number,
options?: FitBoundsOptions,
eventData?: any,
): thisPans, rotates and zooms the map to to fit the box made by points p0 and p1 once the map is rotated to the specified bearing. To zoom without rotating, pass in the current map bearing.
Triggers the following events:
movestart,move,moveend,zoomstart,zoom,zoomendandrotate.Parameters
- p0: PointLike
First point on screen, in pixel coordinates
- p1: PointLike
Second point on screen, in pixel coordinates
- bearing: number
Desired map bearing at end of animation, in degrees
Optionaloptions: FitBoundsOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
Example
let p0 = [220, 400]; let p1 = [500, 900]; map.fitScreenCoordinates(p0, p1, map.getBearing(), { padding: {top: 10, bottom:25, left: 15, right: 5} });See
Used by BoxZoomHandler
- p0: PointLike
flyTo
Changes any combination of center, zoom, bearing, pitch, and roll, animating the transition along a curve that evokes flight. The animation seamlessly incorporates zooming and panning to help the user maintain her bearings even after traversing a great distance.
!!! note "Reduced Motion" The animation will be skipped, and this will behave equivalently to
jumpToif the user has thereduced motionaccessibility feature enabled in their operating system, unless 'options' includesessential: true.Triggers the following events:
movestart,move,moveend,zoomstart,zoom,zoomend,pitchstart,pitch,pitchend,rollstart,roll,rollend, androtate.Parameters
- options: FlyToOptions
Options describing the destination and animation of the transition. Accepts CameraOptions, AnimationOptions, and the following additional options.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- options: FlyToOptions
getBearing
Returns the map's current bearing. The bearing is the compass direction that is "up"; for example, a bearing of 90° orients the map so that east is up.
Returns number
The map's current bearing.
getCenter
getCenterClampedToGround
Returns the value of
centerClampedToGround.If true, the elevation of the center point will automatically be set to the terrain elevation (or zero if terrain is not enabled). If false, the elevation of the center point will default to sea level and will not automatically update. Defaults to true. Needs to be set to false to keep the camera above ground when pitch > 90 degrees.
Returns boolean
getCenterElevation
Returns the elevation of the map's center point.
Returns number
The elevation of the map's center point, in meters above sea level.
getPadding
Returns the current padding applied around the map viewport.
Returns PaddingOptions
The current padding around the map viewport.
getPitch
Returns the map's current pitch (tilt).
Returns number
The map's current pitch, measured in degrees away from the plane of the screen.
getRoll
Returns the map's current roll angle.
Returns number
The map's current roll, measured in degrees about the camera boresight.
getVerticalFieldOfView
getZoom
getZoomSnap
Returns the map's current zoom snap level.
Returns number
The map's current zoom snap level.
isEasing
Returns boolean
jumpTo
Changes any combination of center, zoom, bearing, pitch, and roll, without an animated transition. The map will retain its current values for any details not specified in
options.Triggers the following events:
movestart,move,moveend,zoomstart,zoom,zoomend,pitchstart,pitch,pitchend,rollstart,roll,rollendandrotate.Parameters
- options: JumpToOptions
Options object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- options: JumpToOptions
listens
Returns a true if this instance of Evented or any forwardeed instances of Evented have a listener for the specified type.
Parameters
- type: string
The event type
Returns boolean
trueif there is at least one registered listener for specified event type,falseotherwise- type: string
migrateProjection
Parameters
- newTransform: ITransform
- newCameraHelper: ICameraHelper
Returns void
off
Removes a previously registered event listener.
Parameters
- type: string
The event type to remove listeners for.
- listener: Listener
The listener function to remove.
Returns this
- type: string
on
Adds a listener to a specified event type.
Parameters
- type: string
The event type to add a listen for.
- listener: Listener
The function to be called when the event is fired. The listener function is called with the data object passed to
fire, extended withtargetandtypeproperties.
Returns Subscription
- type: string
once
Adds a listener that will be called only once to a specified event type.
The listener will be called first time the event fires after the listener is registered.
Parameters
- type: string
The event type to listen for.
Optionallistener: ListenerThe function to be called when the event is fired the first time.
Returns Camera | Promise<any>
thisor a promise if a listener is not provided- type: string
panBy
Pans the map by the specified offset.
Triggers the following events:
movestartandmoveend.Parameters
- offset: PointLike
xandycoordinates by which to pan the map. Optionaloptions: EaseToOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- offset: PointLike
panTo
Pans the map to the specified location with an animated transition.
Triggers the following events:
movestartandmoveend.Parameters
- lnglat: LngLatLike
The location to pan the map to.
Optionaloptions: EaseToOptionsOptions describing the destination and animation of the transition.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- lnglat: LngLatLike
queryTerrainElevation
Gets the elevation at a given location, in meters above sea level. Returns null if terrain is not enabled. If terrain is enabled with some exaggeration value, the value returned here will be reflective of (multiplied by) that exaggeration value. This method should be used for proper positioning of custom 3d objects, as explained here
Parameters
- lngLatLike: LngLatLike
[x,y] or LngLat coordinates of the location
Returns number | null
elevation in meters
- lngLatLike: LngLatLike
resetNorth
Rotates the map so that north is up (0° bearing), with an animated transition.
Triggers the following events:
movestart,moveend, androtate.Parameters
Optionaloptions: AnimationOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
resetNorthPitch
Rotates and pitches the map so that north is up (0° bearing) and pitch and roll are 0°, with an animated transition.
Triggers the following events:
movestart,move,moveend,pitchstart,pitch,pitchend,rollstart,roll,rollend, androtate.Parameters
Optionaloptions: AnimationOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
rotateTo
Rotates the map to the specified bearing, with an animated transition. The bearing is the compass direction that is "up"; for example, a bearing of 90° orients the map so that east is up.
Triggers the following events:
movestart,moveend, androtate.Parameters
- bearing: number
The desired bearing.
Optionaloptions: EaseToOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- bearing: number
setBearing
Sets the map's bearing (rotation). The bearing is the compass direction that is "up"; for example, a bearing of 90° orients the map so that east is up.
Equivalent to
jumpTo({bearing: bearing}).Triggers the following events:
movestart,moveend, androtate.Parameters
- bearing: number
The desired bearing.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- bearing: number
setCenter
Sets the map's geographical centerpoint. Equivalent to
jumpTo({center: center}).Triggers the following events:
movestartandmoveend.Parameters
- center: LngLatLike
The centerpoint to set.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- center: LngLatLike
setCenterClampedToGround
Sets the value of
centerClampedToGround.If true, the elevation of the center point will automatically be set to the terrain elevation (or zero if terrain is not enabled). If false, the elevation of the center point will default to sea level and will not automatically update. Defaults to true. Needs to be set to false to keep the camera above ground when pitch > 90 degrees.
Parameters
- centerClampedToGround: boolean
Returns void
setCenterElevation
Sets the elevation of the map's center point, in meters above sea level. Equivalent to
jumpTo({elevation: elevation}).Triggers the following events:
movestartandmoveend.Parameters
- elevation: number
The elevation to set, in meters above sea level.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- elevation: number
setEventedParent
Bubble all events fired by this instance of Evented to this parent instance of Evented.
Parameters
Optionalparent: Evented | nullOptionaldata: any
Returns this
setPadding
Sets the padding in pixels around the viewport.
Equivalent to
jumpTo({padding: padding}).Triggers the following events:
movestartandmoveend.Parameters
- padding: PaddingOptions
The desired padding.
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- padding: PaddingOptions
setPitch
Sets the map's pitch (tilt). Equivalent to
jumpTo({pitch: pitch}).Triggers the following events:
movestart,moveend,pitchstart, andpitchend.Parameters
- pitch: number
The pitch to set, measured in degrees away from the plane of the screen (0-60).
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- pitch: number
setRoll
Sets the map's roll angle. Equivalent to
jumpTo({roll: roll}).Triggers the following events:
movestart,moveend,rollstart, androllend.Parameters
- roll: number
The roll to set, measured in degrees about the camera boresight
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- roll: number
setVerticalFieldOfView
Sets the map's vertical field of view, in degrees.
Triggers the following events:
movestart,move, andmoveend.Parameters
- fov: number
The vertical field of view to set, in degrees (0-180).
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- fov: number
setZoom
Sets the map's zoom level. Equivalent to
jumpTo({zoom: zoom}).Triggers the following events:
movestart,move,moveend,zoomstart,zoom, andzoomend.Parameters
- zoom: number
The zoom level to set (0-20).
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- zoom: number
setZoomSnap
Sets the map's zoom snap level.
Parameters
- snap: number
The zoom snap level to set.
Returns this
- snap: number
snapToNorth
Snaps the map so that north is up (0° bearing), if the current bearing is close enough to it (i.e. within the
bearingSnapthreshold).Triggers the following events:
movestart,moveend, androtate.Parameters
Optionaloptions: AnimationOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
stop
Stops any animated transition underway.
Returns this
zoomIn
Incrementally increases the map's zoom level by 1, first snapping to the nearest
zoomSnapincrement.Triggers the following events:
movestart,move,moveend,zoomstart,zoom, andzoomend.Parameters
Optionaloptions: AnimationOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
zoomOut
Decreases the map's zoom level by 1, first snapping to the nearest
zoomSnapincrement.Triggers the following events:
movestart,move,moveend,zoomstart,zoom, andzoomend.Parameters
Optionaloptions: AnimationOptionsOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
zoomTo
Zooms the map to the specified zoom level, with an animated transition.
Triggers the following events:
movestart,move,moveend,zoomstart,zoom, andzoomend.Parameters
- zoom: number
The zoom level to transition to.
Optionaloptions: EaseToOptions | nullOptions object
OptionaleventData: anyAdditional properties to be added to event objects of events triggered by this method.
Returns this
- zoom: number
Methods mixed in to other classes for event capabilities.