我正在使用mapbox gl
中的mapbox
方向,这在地图框文档的示例中使用。
但是,我无法获得用户输入的地址值。
我需要用户选择的地址。谢谢!
这是我的代码:
<script>
mapboxgl.accessToken = 'MY TOKEN ACCESS';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-79.4512, 43.6568],
zoom: 13
});
map.addControl(
new MapboxDirections({
accessToken: mapboxgl.accessToken
}),
'top-left'
);
</script>
// Docs for route event is here:
// https://github.com/mapbox/mapbox-gl-directions/blob/master/API.md#on`enter code here`
directions.on('route', e => {
// routes is an array of route objects as documented here:
// https://docs.mapbox.com/api/navigation/#route-object:
let routes = e.route
routes.map(r => r.legs[0].steps[0].name)//get the origin
tail=routes.map(r => r.legs[0].steps.length)//get length of instructions
routes.map(r => r.legs[0].steps[tail-1].name)//get destination
});
mapbox-gl-directions plugin提供getOrigin
和getOrigin
方法,分别用于检索当前路线的起点和终点。