Setting the file. One moment.
Skill 06 · Mapbox Google Maps Migration
Subchapter 6.4
references/directions-controls.mdMarkdown2 KBView on GitHub
const directionsService = new google.maps.DirectionsService();
const directionsRenderer = new google.maps.DirectionsRenderer();
directionsRenderer.setMap(map);
directionsService.route(
{
origin: 'San Francisco, CA',
destination: 'Los Angeles, CA',
travelMode: 'DRIVING'
},
(response, status) => {
if (status === 'OK') {
directionsRenderer.setDirections(response);
}
}
);// Use Mapbox Directions API
const origin = [-122.4194, 37.7749];
const destination = [-118.2437, 34.0522];
fetch(
`https://api.mapbox.com/directions/v5/mapbox/driving/${origin.join(',')};${destination.join(',')}?geometries=geojson&access_token=${mapboxgl.accessToken}`
)
.then((response) => response.json())
.then((data) => {
const route = data.routes[0].geometry;
map.addSource('route', {
type: 'geojson',
data: {
type: 'Feature',
geometry: route
}
});
map.addLayer({
id: 'route',
type: 'line',
source: 'route',
paint: {
'line-color': '#3887be',
'line-width': 5
}
});
});
// Or use @mapbox/mapbox-gl-directions plugin
const directions = new MapboxDirections({
accessToken: mapboxgl.accessToken
});
map.addControl(directions, 'top-left');// Controls are automatic, can configure:
map.setOptions({
zoomControl: true,
mapTypeControl: true,
streetViewControl: false,
fullscreenControl: true
});// Add controls explicitly
map.addControl(new mapboxgl.NavigationControl()); // Zoom + rotation
map.addControl(new mapboxgl.FullscreenControl());
map.addControl(new mapboxgl.GeolocateControl());
map.addControl(new mapboxgl.ScaleControl());
// Position controls
map.addControl(new mapboxgl.NavigationControl(), 'top-right');