java 高德地图路线规划_高德地图api之路线规划
1.引入
2.創建并初始化實例對象
const map = new AMap.Map("container")
3.創建一個導航元素,用來承載導航路線(這里不要忘了,是個坑,用的啥時候踩過,文檔里沒有詳細說)
4.使用路線規劃插件
這里先拿駕車路線規劃舉個例子
假設var start = "天安門", end = "人民大會堂"
AMap.plugin('AMap.Driving', function () {
const driving = new AMap.Driving({
// 駕車路線規劃策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
policy: AMap.DrivingPolicy.LEAST_TIME,
// map 指定將路線規劃方案繪制到對應的AMap.Map對象上
map: map,
// panel 指定將結構化的路線詳情數據顯示的對應的DOM上,傳入值需是DOM的ID
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
// 搜索完成后,將自動繪制路線到地圖上
driving.search(points)
})
當然不只有駕車路線規劃,還有其他類型:
步行規劃
AMap.plugin('AMap.Walking', function () {
const walking = new AMap.Walking({
map: map,
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
walking.search(points)
})
騎行規劃
AMap.plugin('AMap.Riding', function () {
const riding = new AMap.Riding({
map: map,
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
riding.search(points)
})
公交規劃
AMap.plugin('AMap.Transfer', function () {
const transfer = new AMap.Transfer({
map: map,
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
transfer.search(points)
})
貨車規劃(一天只能調用100次,超過收費)
AMap.plugin('AMap.TruckDriving', function () {
const truckDriving = new AMap.TruckDriving({
map: map,
panel: 'panel',
// policy: ,
size: 1, // 必填, 車輛大小
})
const points = [
{ keyword: start },
{ keyword: end }
]
truckDriving.search(points)
})
以上只是簡單的實現,具體的屬性和方法還是要看文檔的
附demo:
Document起始點:
目的地:
步行規劃
騎行規劃
駕車規劃
公交規劃
貨車規劃
const map = new AMap.Map('container')
function gui(tab) {
const start = document.getElementById('start').value
const end = document.getElementById('end').value
switch (tab) {
case 1:
walking(start, end)
break;
case 2:
riding(start, end)
break;
case 3:
driving(start, end)
break;
case 4:
transfer(start, end)
break;
case 5:
truckDriving(start, end)
break;
}
}
// 步行規劃
function walking(start, end) {
AMap.plugin('AMap.Walking', function () {
const walking = new AMap.Walking({
map: map,
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
walking.search(points)
})
}
// 騎行規劃
function riding(start, end) {
AMap.plugin('AMap.Riding', function () {
const riding = new AMap.Riding({
map: map,
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
riding.search(points)
})
}
// 駕車規劃
function driving(start, end) {
AMap.plugin('AMap.Driving', function () {
const driving = new AMap.Driving({
// 駕車路線規劃策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
policy: AMap.DrivingPolicy.LEAST_TIME,
// map 指定將路線規劃方案繪制到對應的AMap.Map對象上
map: map,
// panel 指定將結構化的路線詳情數據顯示的對應的DOM上,傳入值需是DOM的ID
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
// 搜索完成后,將自動繪制路線到地圖上
driving.search(points)
})
}
// 公交規劃
function transfer(start, end) {
AMap.plugin('AMap.Transfer', function () {
const transfer = new AMap.Transfer({
map: map,
panel: 'panel'
})
const points = [
{ keyword: start },
{ keyword: end }
]
transfer.search(points)
})
}
// 貨車路徑規劃
function truckDriving(start, end) {
AMap.plugin('AMap.TruckDriving', function () {
const truckDriving = new AMap.TruckDriving({
map: map,
panel: 'panel',
// policy: ,
size: 1, // 必填, 車輛大小
})
const points = [
{ keyword: start },
{ keyword: end }
]
truckDriving.search(points)
})
}
總結
以上是生活随笔為你收集整理的java 高德地图路线规划_高德地图api之路线规划的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: createjs基础入门
- 下一篇: 巴贝奇——筹划信息时代