高德地图——驾驶路线

高德地图——驾驶路线
插件:AMap.Driving
在这里插入图片描述
方法一:规定路线

new AMap.Driving({
    map:map,
    panel:'panel'
    }).search([
        {keyword:起点,city:'北京'},
        {keyword:终点,city:'北京'}
    ],function(status,data){
        console.log(data);
 })
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=34614f775ec9455cf2f9a5c7bb6acfa0&plugin=AMap.Driving,AMap.Autocomplete"></script>
<style type="text/css">
        *{margin: 0;padding: 0;list-style: none;}
    	#container{width: 100%;height: 100%;position: absolute;left: 0;top: 0;}
        #panel{position: fixed;background: white;top: 10px;right: 10px;width: 280px;}
        #search{width: 200px;height: 100px;position: absolute;left: 10px;right: 10px;background: white;}
</style>
    </head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>
    <div id="search">
        起点:<input type="text" name="" id="startNode"><br>
        终点:<input type="text" name="" id="endNode"><br>
        <button id="btn">开始导航</button>
    </div>
    <script type="text/javascript">
		var map = new AMap.Map('container',{
            zoom:11,
            center:[116.379391,39.861536],
        });

        new AMap.Autocomplete({
            input:'startNode'
        });
        new AMap.Autocomplete({
            input:'endNode'
        });
        btn.onclick = function(){
            new AMap.Driving({
                map:map,
                panel:'panel'
            }).search([
                {keyword:startNode.value,city:'北京'},
                {keyword:endNode.value,city:'北京'}
            ],function(status,data){
                console.log(data);
            })
        }
	</script> 
</body>
</html>

方法二:点击地图上任意两点,通过经纬度来进行导航

new AMap.Driving({
    map:map,
    panel:'panel'
    }).search(new AMap.LngLat(startX,startY),new AMap.LngLat(endX,endY),function(status,data){
        console.log(data);
})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=34614f775ec9455cf2f9a5c7bb6acfa0&plugin=AMap.Driving,AMap.Autocomplete"></script>
<style type="text/css">
        *{margin: 0;padding: 0;list-style: none;}
    	#container{width: 100%;height: 100%;position: absolute;left: 0;top: 0;}
        #panel{position: fixed;background: white;top: 10px;right: 10px;width: 280px;}
        #search{width: 200px;height: 100px;position: absolute;left: 10px;right: 10px;background: white;}
</style>
    </head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>
    <div id="search">
        起点:<input type="text" name="" id="startNode"><br>
        终点:<input type="text" name="" id="endNode"><br>
        <button id="btn">开始导航</button>
    </div>
    <script type="text/javascript">
		var map = new AMap.Map('container',{
            zoom:11,
            center:[116.379391,39.861536],
        });

        new AMap.Autocomplete({
            input:'startNode'
        });
        new AMap.Autocomplete({
            input:'endNode'
        });

        var num =0,arr=[];
        map.on('click',function(e){
            num++;
            if(num%2==1){
                arr=[e.lnglat.Q,e.lnglat.P];
            }else{
                new AMap.Driving({
                    map:map,
                    panel:'panel'
                }).search(new AMap.LngLat(arr[0],arr[1]),new AMap.LngLat(e.lnglat.Q,e.lnglat.P),
                function(status,data){
                    console.log(data);
                })
            }
        });
	</script>   
</body>
</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实时路线轨迹的实现需要结合高德地图的定位功能和绘制功能。 1. 首先,在Vue中引入高德地图的JavaScript API,并创建地图对象: ```javascript import AMapLoader from '@amap/amap-jsapi-loader' // 创建地图对象 let map = null AMapLoader.load({ key: 'your amap key', version: '2.0', plugins: ['AMap.Geolocation'] }).then((AMap) => { map = new AMap.Map('map-container') }) ``` 2. 调用高德地图的定位功能获取当前位置: ```javascript let geolocation = null // 创建定位对象 AMapLoader.load({ key: 'your amap key', version: '2.0', plugins: ['AMap.Geolocation'] }).then((AMap) => { geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为false timeout: 10000, // 超过10秒后停止定位,默认为无穷大 buttonOffset: new AMap.Pixel(10, 20), // 定位按钮的位置偏移量 }) map.addControl(geolocation) // 获取当前位置 geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,将地图中心设置为当前位置 map.setCenter(result.position) } else { // 定位失败 console.log('定位失败', result.message) } }) }) ``` 3. 在地图上绘制路线轨迹: ```javascript let path = [] // 路线轨迹点集合 // 添加路线轨迹 let polyline = new AMap.Polyline({ path, strokeColor: '#00a1fe', strokeWeight: 5, }) polyline.setMap(map) // 更新路线轨迹 function updatePath(newPoint) { path.push(newPoint) polyline.setPath(path) } ``` 4. 实时更新路线轨迹: ```javascript // 开启定时器,每隔1秒更新路线轨迹 setInterval(() => { // 获取当前位置 geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,更新路线轨迹 updatePath(result.position) } else { // 定位失败 console.log('定位失败', result.message) } }) }, 1000) ``` 以上就是Vue高德地图实现实时路线轨迹的基本流程,需要注意的是,高德地图的JavaScript API使用了Promise异步机制,需要注意回调函数的执行时机。另外,在实际应用中,还需要结合后端接口获取历史路线轨迹数据,并将其加载到地图上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值