在高德地图上添加折线

一条直线有两个端点确定,所以想要在地图上添加一条可展示的直线或折线,需要明确点位的具体经纬度,只需要调用高德api即可完成

1.先获取精准的点位坐标 

可以通过高德的点击事件获取点击位置的坐标,例如:

this.map.on('click', function(e) {
  console.log(e)
});

 

let poly_arr = [
          [
            [120.155818,36.859481],
            [120.156534,36.858955],
          ],
          [
            [120.156422,36.860002],
            [120.157115,36.859468],
          ],
          [
            [120.157014,36.860535],
            [120.157716,36.860022],
          ],
          [
            [120.157614,36.861057],
            [120.15833, 36.860561],
          ],

        ]
       

2.通过循环统一生成折线


        for(let i = 0; i < poly_arr.length; i++){
          var polyline = new AMap.Polyline({
            path: poly_arr[i],
            isOutline: true,
            outlineColor: "#ffeeff",
            borderWeight: 1,
            strokeColor: "#ffffff",
            strokeOpacity: 1,
            strokeWeight: 6,
            // 折线样式还支持 'dashed'
            strokeStyle: "solid",
            // strokeStyle是dashed时有效
            strokeDasharray: [10, 5],
            lineJoin: "round",
            lineCap: "round",
            zIndex: 50,
            zooms:[16,20]
          });
          this.map.add(polyline);
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中使用高德地图手动绘制折线,可以借助上面提到的高德地图 JavaScript API 中提供的绘制工具库来实现。以下是一个简单的实现方法: 1. 在 Vue 中引入高德地图 JavaScript API 和绘制工具库: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=YOUR_KEY"></script> <script src="https://webapi.amap.com/ui/1.0/main.js"></script> ``` 2. 在 Vue 中创建地图实例和绘制工具实例: ```javascript export default { data() { return { map: null, drawTool: null, polyline: null, }; }, mounted() { this.initMap(); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); this.drawTool = new AMap.MouseTool(this.map); }, }, }; ``` 3. 在 Vue 中添加绘制完成事件监听器并处理绘制结果: ```javascript export default { methods: { // ... drawPolyline() { this.drawTool.polyline(); this.drawTool.on('draw', this.handleDrawPolyline); }, handleDrawPolyline(e) { const path = e.obj.getPath(); console.log('绘制完成', path); // 处理绘制结果 this.polyline = new AMap.Polyline({ path, strokeColor: '#3366FF', strokeWeight: 5, }); this.polyline.setMap(this.map); // 移除事件监听器 this.drawTool.off('draw', this.handleDrawPolyline); }, }, }; ``` 4. 在 Vue 中调用绘制方法开始绘制: ```html <template> <div id="map-container"></div> <button @click="drawPolyline">绘制折线</button> </template> ``` ```javascript export default { methods: { // ... }, }; ``` 以上是一个简单的实现方法,你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值