gpt4 book ai didi

javascript - 如何预填充 mapbox directions api 的起点和终点?

转载 作者:行者123 更新时间:2023-11-30 09:21:11 25 4
gpt4 key购买 nike

我有:

<!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>

这给了我一张带有行车路线的漂亮 map ,但我想在呈现的 map 中预填“起点”和“目的地”。我如何通过 javascript 做到这一点,以便用户不必输入他们的信息两次?

最佳答案

您需要 setOriginsetDestination来自 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/ ]

关于javascript - 如何预填充 mapbox directions api 的起点和终点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51530395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com