高德地图-----驾车路线

准备工作

  1. 在public中的index.html中初始化地图
<script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
  1. 在 .eslintrc.js 中配置AMap和AMapUI
globals: {
    "AMap": "true",
    "AMapUI":"true",
  }

实现思路

  1. 封装一个全局组件scottMap.vue,用于加载地图
  2. 封装一个基于scottMap.vue的组件mapDriveLine.vue,用于在地图上画出驾车路线
  3. 通过v-if和组件传参,确定需要的地图组件
  4. 组件嵌套封装代码(一个地图上面有多个功能),相比一个地图上封装一个功能,这种方式加载速度更快

scottMap.vue

<template>
  <div class="m-map">
    <div id="js-container" class="map">正在加载数据 ...</div>
    <div class="route">
        <map-drive-line v-if="isDriveLine" :map="map" :AMap="AMap"></map-drive-line>
    </div>
  </div>
</template>

<script>
import MapDriveLine from './scottMap/mapDriveLine.vue'
/*
  传递参数:
    1. isDriveLine:   是否需要驾车路线
*/
export default {
  name: 'mapIndex',
  components: { MapDriveLine },
  props: {
    isDriveLine: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      AMap: null,
      map: null
    }
  },
  mounted () {
    this.initMap()
  },
  methods: {
    // 实例化地图
    initMap () {
      this.AMap = window.AMap
      this.map = new AMap.Map('js-container', {
        zoom: 10, // 设置地图显示的缩放级别
        center: [117.27, 31.86] // 设置地图中心点坐标
      })
    }

  }

}
</script>

<style lang="css">
.m-map{ min-width: 500px; min-height: 300px; position: relative; }
.m-map .map{ width: 100%; height: 100%; }
.route{
    position: absolute;
    top: 10px;
    left: 200px;
    display: flex;
}
</style>

mapDriveLine.vue

<template>
    <div>
        <el-button @click="drawRoute2">绘制路线2</el-button>
        <el-button @click="closeRoute2">关闭路线2</el-button>
    </div>
</template>

<script>
export default {
  name: 'MapDriveLine',
  props: ['map', 'AMap'],
  components: {},
  data () {
    return {
      driving: null
    }
  },
  methods: {
    // 使用驾车路线
    drawRoute2 () {
      const that = this
      that.AMap.plugin('AMap.Driving', function () {
        if (!that.driving) {
          that.driving = new AMap.Driving({
            map: that.map,
            showTraffic: false,
            // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
            policy: that.AMap.DrivingPolicy.LEAST_TIME
          })

          var startLngLat = [114.078454, 22.579593]
          var endLngLat = [114.082166, 22.57676]

          that.driving.search(startLngLat, endLngLat, function (status, result) {})
        }
      })
    },
    // 关闭驾车路线
    closeRoute2 () {
      if (this.driving) {
        this.driving.clear()
        this.driving = null
      }
    }
  }
}
</script>

<style lang='less' scoped>

</style>

页面调用

 <scottMap class="scottMap" :isDriveLine="true"></scottMap>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
高德地图提供了 JavaScript API,可以在网页中嵌入地图,并自定义地图的样式和功能。其中,绘制路线图也非常简单,可以使用高德地图的 `AMap.Driving` 类实现。 以下是一个简单的 HTML 代码示例,用于绘制两个地点之间的驾车路线图: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>高德地图路线图示例</title> <!-- 引入高德地图 JavaScript API --> <script src="https://webapi.amap.com/maps?v=1.4.15&key=您的API密钥"></script> <style> #map { width: 100%; height: 500px; } </style> </head> <body> <!-- 地图容器 --> <div id="map"></div> <script> // 创建地图实例 var map = new AMap.Map('map', { center: [116.397428, 39.90923], // 地图中心点 zoom: 13 // 地图缩放级别 }); // 创建驾车路线规划服务实例 var driving = new AMap.Driving({ policy: AMap.DrivingPolicy.LEAST_TIME // 路线规划策略:最短时间 }); // 根据起点和终点坐标,绘制驾车路线图 driving.search([ {keyword: '起点地址'}, {keyword: '终点地址'} ], function(status, result) { if (status === 'complete' && result.info === 'OK') { // 绘制路线图 var path = result.routes[0].path; var polyline = new AMap.Polyline({ path: path, isOutline: true, outlineColor: '#ffeeee', borderWeight: 2, strokeColor: '#3366ff', strokeOpacity: 0.7, strokeWeight: 5 }); polyline.setMap(map); // 调整地图视野,使路线图可见 map.setFitView(polyline); } else { alert('路线规划失败:' + result.info); } }); </script> </body> </html> ``` 在上面的代码中,需要替换 `您的API密钥` 和 `起点地址`、`终点地址` 三个地方的内容。其中,API密钥可以在[高德开放平台](https://lbs.amap.com/)申请获得。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值