Modify the camera's field of view
This example uses setVerticalFieldOfView() to adjust the map camera's vertical field of view, producing telephoto or wide-angle effects. Horizontal field of view is not independently controllable — it's derived from the vertical field of view and the Map's aspect ratio, and is available as a read-only value via getHorizontalFieldOfView().
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modify the camera's field of view</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.30.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.30.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
.controls {
position: absolute;
top: 20px;
left: 20px;
background: rgba(255, 255, 255, 0.95);
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
sans-serif;
z-index: 1;
min-width: 250px;
}
.controls h3 {
margin: 0 0 15px 0;
font-size: 16px;
color: #333;
}
.controls span {
min-width: 44px;
display: inline-block;
}
.slider-group label {
display: block;
margin-bottom: 5px;
font-size: 14px;
color: #555;
}
.slider-group input[type='range'] {
width: 100%;
cursor: pointer;
}
.value {
font-weight: bold;
color: #0066cc;
}
</style>
<div class="controls">
<div class="slider-group">
<input type="range" id="fov-slider" min="1" max="60" step="0.1" value="36.87">
<label><strong>Vertical Field of View:</strong>
<span id="v-fov-value" class="value">36.87</span></label>
<label><strong>Horizontal Field of View</strong> (derived):
<span id="h-fov-value" class="value">64.01</span></label>
</div>
</div>
<!-- Map -->
<div id="map"></div>
<script>
const map = new mapboxgl.Map({
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
container: 'map',
config: {
basemap: {
showPlaceLabels: false,
showPointOfInterestLabels: false,
showRoadLabels: false
}
},
center: [-71.06288, 42.36834],
zoom: 16.85,
bearing: 141.6,
pitch: 75.67
});
// FOV slider
document.getElementById('fov-slider').addEventListener('input', (e) => {
const fov = parseFloat(e.target.value);
document.getElementById('v-fov-value').textContent = fov.toFixed(2);
document.getElementById('h-fov-value').textContent = map
.getHorizontalFieldOfView()
.toFixed(2);
map.setVerticalFieldOfView(fov);
});
</script>
</body>
</html>
This code snippet will not work as expected until you replace
YOUR_MAPBOX_ACCESS_TOKEN with an access token from your Mapbox account.import React, { useEffect, useRef, useState } from 'react';
import * as mapboxgl from 'mapbox-gl/esm';
import 'mapbox-gl/dist/mapbox-gl.css';
const MapboxExample = () => {
const mapContainerRef = useRef();
const mapRef = useRef();
const [fov, setFov] = useState({ vertical: 36.87, horizontal: 64.01 });
const labelStyles = {
display: 'block',
marginBottom: 5,
fontSize: 14,
color: '#555'
};
const spanStyles = {
minWidth: 44,
display: 'inline-block',
fontWeight: 'bold',
color: '#0066cc',
marginLeft: 5
};
const handleFov = (e) => {
mapRef.current.setVerticalFieldOfView(e.target.value);
setFov({
vertical: mapRef.current.getVerticalFieldOfView().toFixed(2),
horizontal: mapRef.current.getHorizontalFieldOfView().toFixed(2)
});
};
useEffect(() => {
mapRef.current = new mapboxgl.Map({
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
container: mapContainerRef.current,
config: {
basemap: {
showPlaceLabels: false,
showPointOfInterestLabels: false,
showRoadLabels: false
}
},
center: [-71.06288, 42.36834],
zoom: 16.85,
bearing: 141.6,
pitch: 75.67
});
}, []);
return (
<>
<div
className="controls"
style={{
position: 'absolute',
top: 20,
left: 20,
background: 'rgba(255, 255, 255, 0.95)',
padding: 20,
borderRadius: 8,
boxShadow: '0 2 10 rgba(0,0,0,0.2)',
fontFamily:
"-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
zIndex: 1,
minWidth: 250
}}
>
<div>
<input
type="range"
style={{ width: '100%', cursor: 'pointer', display: 'block' }}
id="fov-slider"
min="1"
max="60"
step="0.1"
value={fov.vertical}
onChange={handleFov}
/>
<div style={labelStyles}>
<strong>Vertical Field of View:</strong>
<span id="v-fov-value" style={spanStyles}>
{fov.vertical}
</span>
</div>
<div style={labelStyles}>
<strong>Horizontal Field of View</strong> (derived):
<span id="h-fov-value" style={spanStyles}>
{fov.horizontal}
</span>
</div>
</div>
</div>
<div ref={mapContainerRef} id="map" style={{ height: '100%' }}></div>
</>
);
};
export default MapboxExample;
This code snippet will not work as expected until you replace
YOUR_MAPBOX_ACCESS_TOKEN with an access token from your Mapbox account.Was this example helpful?