Reputation: 3888
I have:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Display driving directions</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v3.1.3/mapbox-gl-directions.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v3.1.3/mapbox-gl-directions.css' type='text/css' />
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiYndhZGFtc29uIiwiYSI6ImNqajZhNm1idDFzMjIza3A2Y3ZmdDV6YWYifQ.9NhptR7a9D0hzWXR51y_9w';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-79.4512, 43.6568],
zoom: 13
});
map.addControl(new MapboxDirections({
accessToken: mapboxgl.accessToken
}), 'top-left');
</script>
</body>
</html>
This gives me a nice map with driving directions but I want to prefill the "starting place" and "destination" in the rendered map. How do I do that via javascript so the user doesn't have to enter their information twice?
Upvotes: 2
Views: 1900
Reputation: 29172
You need the setOrigin
and setDestination
methods from the MapboxDirections API
:
map.on('load', function() {
var directions = new MapboxDirections({
accessToken: mapboxgl.accessToken
});
map.addControl(directions, 'top-left');
directions.setOrigin('Brockton Avenue, Toronto');
directions.setDestination('East York Avenue, Toronto');
});
[ https://jsfiddle.net/me3kj9td/ ]
Upvotes: 6