高德地图规划路线编写

1、创建承载地图的dom元素

 
<style>
        #container {
            height: 80%;
            width: 80%;
        }
 
</style>

 <style type="text/css">
        #panel {
            position: fixed;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }

        #panel .amap-call {
            background-color: #009cf9;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }

        #panel .amap-lib-driving {
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
            overflow: hidden;
        }
    </style>




<div id="container"></div>
 <div id="panel"></div>

2、引入需要的一些插件

 <!-- 加载地图JSAPI脚本 -->
    <script type="text/javascript">
        window._AMapSecurityConfig={
            securityJsCode: '2badf6d16a37da1a7ba6654dc816dc0f',
        };
    </script>
    <!-- 高德的loader -->
    <script src="https://webapi.amap.com/loader.js"></script>

3、实例化地图

 AMapLoader.load({
            key: '8afe356ee5bb2ef0b4ac7a37c380c804', // 申请好的Web端开发者Key,首次调用 load                                             时必填
            version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        }).then((AMap) => {

//所有代码都需要在.then中写 因为都是基于它的AMap操作的
 })

4、直接编写

const map=new AMap.Map('container',{
                    resizeEnable: true,
                    center: [116.397428,39.90923],//地图中心点
                    zoom: 13 //地图显示的缩放级别
                });
                //构建路线的函数
                const luxian=function() {
                    // 声明的起点和终点
                    // 数组第一项为起点 第二项为终点
                    let arr=[
                        {keyword: '积云',city: '北京'},
                        {keyword: '北大',city: '北京'}
                    ]
                    AMap.plugin(['AMap.Driving'],function() {
                        driving=new AMap.Driving(map)
                        //构造路线导航类
                        driving=new AMap.Driving({
                            map: map,
                            panel: "panel"
                        });

                        driving.search(
                            arr,
                            function(status,result) {
                                // result 即是对应的驾车导航信息,相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
                                if(status==='complete') {
                                    console.log('绘制驾车路线完成');
                                } else {

                                    console.log('获取驾车数据失败:'+result);
                                }
                            });
                    })

                }
                luxian()

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值