因此,我正在編寫一個應用程序,它允許管理員用戶創建一個具有不同停靠點的特定位置的旅程。Mapbox gl directions API
用於顯示地圖,添加標記,flyTo位置等我正在使用Mapbox GL
。
我用cURL
實施Mapbox
API來獲取行車路線,然後繪製地圖
一行所以作爲cURL
調用的一個例子,我收到這代表我的方向座標列表。
問題是在我試圖在地圖上連接這些點時出現的。
由於HTML
一些JS
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></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.18.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.18.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = '<ACCESS TOKEN>';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v8',
center: [-122.486052, 37.830348],
zoom: 15
});
map.on('load', function() {
map.addSource("route", {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-155.088899,19.722942],[-155.08565,19.72472],[-155.084661,19.723701],[-155.083569,19.723139],[-155.079557,19.722262],[-155.074227,19.721938],[-155.069939,19.722545],[-155.070061,19.721225],[-155.07007,19.711726]
]
}
}
});
map.addLayer({
"id": "route",
"type": "line",
"source": "route",
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "#888",
"line-width": 8
}
});
});
</script>
</body>
</html>
你可以看到一組將被連接到畫一條線座標的一個例子。我想知道是否有連接這些點的方法,以便線路只會跟隨(駕駛)道路?
我知道這是我的問題是通用解釋的親密變焦,但我希望這是可以理解的。
我一直在試圖做一些神奇的Mapbox Gl Directions API
,但沒有運氣,因爲我不得不添加一個contoller
,我不想。我只需要繪製路線並且不允許公共用戶修改它。
任何建議?
嗯,有趣的方法,雖然對我來說這聽起來像一個黑客攻擊。我也想要的是按照這種模式創建類似於A-> B-> C-> D的路由的可擴展性。但我仍然喜歡你的方式! –
@AndrejsGubars:我更新了上面的答案,並提供了更多有關添加路標的註釋,正如您所要求的。還添加了一些關於製作解決方案的註釋。希望有所幫助。 –
是的,現貨!那正是我想要做的。我想我太野心勃勃,期待整個圖書館能做些什麼:)但是,謝謝你的建議! –