vue-baidu-map通过路书实现轨迹回放

Laughing
2021-06-08 / 4 评论 / 3,018 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年03月17日,已超过306天没有更新,若内容或图片失效,请留言反馈。

网上实现轨迹回放的代码很多,但是很少有vue实现的。
本文通过vue-baidu-map对vue轨迹回放功能进行说明。

安装依赖

npm install vue-baidu-map --save

修改main.js

main.js增加以下内容,注意ak需要替换成自己的。

import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
  ak: '替换成自己的ak'
})

前端页面

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-row>
        <el-col :span="6">
          <el-form-item label="车辆" prop="vehicleLicence">
            <el-select filterable v-model="queryParams.vehicleLicence" placeholder="请选择车辆" clearable size="small"
                       style="width: 100%"
            >
              <el-option
                v-for="vehicle in vehicleOptions" :key="vehicle.licence" :label="vehicle.licence"
                :value="vehicle.licence"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="查询时间" prop="dateRange">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 100%"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="-"
              start-placeholder="请选择开始时间"
              end-placeholder="请选择结束时间"
              :picker-options="pickerOptions"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="轨迹速度">
            <el-slider v-model="speed" style="width: 200px" :step="1"></el-slider>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item style="float: right">
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <baidu-map style="height:47.1rem;width:100%" :center="center" :zoom="15" :scroll-wheel-zoom="true">
      <bm-polyline :path="path" stroke-color="blue" :stroke-opacity="0.5" :stroke-weight="3" :editing="false"
      ></bm-polyline>
      <bm-marker :position="{lng: startMark.lng, lat: startMark.lat}"></bm-marker>
      <bm-marker :position="{lng: endMark.lng, lat: endMark.lat}"></bm-marker>
      <bml-lushu
        @stop="reset"
        :path="path"
        :icon="icon"
        :play="play"
        :rotation="true"
        :speed="speed * 10"
        :infoWindow="true"
        :content="content"
      >
      </bml-lushu>
    </baidu-map>
  </div>
</template>

<script>
//百度地图
import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
import BmScale from 'vue-baidu-map/components/controls/Scale'
import BmNavigation from 'vue-baidu-map/components/controls/Navigation'
import BmMarkerClusterer from 'vue-baidu-map/components/extra/MarkerClusterer'
import BmMarker from 'vue-baidu-map/components/overlays/Marker'
import BmInfoWindow from 'vue-baidu-map/components/overlays/InfoWindow'
import { BmlLushu } from 'vue-baidu-map'
import drugMarkerIcon from '@/assets/icons/map_marker_check.png'
import { selectGpsOrbitList } from '@/api/dmp/industry/gps/orbit'
import { listAllVehicle } from '@/api/basic/vehicle'

export default {
  components: {
    BaiduMap,
    BmScale,
    BmNavigation,
    BmMarkerClusterer,
    BmMarker,
    BmInfoWindow,
    BmlLushu
  },
  props: {},
  data() {
    return {
      // 查询参数
      queryParams: {
        vehicleLicence: undefined
      },
      // 日期范围
      dateRange: [new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + ' 00:00:00', new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + ' 23:59:59'],
      play: false,
      path: [],
      center: {
        lng: 116.984646,
        lat: 36.659686
      },
      startMark: {},
      endMark: {},
      icon: {
        url: drugMarkerIcon,
        size: { width: 32, height: 32 },
        opts: { anchor: { width: 27, height: 13 } }
      },
      content: undefined,
      speed: 20,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 8.64e6
        }
      },
      vehicleOptions: []
    }
  },
  methods: {
    reset() {
      this.play = false
    },
    handleSearchComplete(res) {
      this.path = res.getPlan(0).getRoute(0).getPath()
    },
    handleQuery() {
      const search = this.addDateRange(this.queryParams, this.dateRange)
      if (this.queryParams.vehicleLicence === undefined) {
        this.msgError('请选择车辆')
        return
      }
      if (search.beginTime === undefined || search.beginTime === '' || search.endTime === undefined || search.endTime === '') {
        this.msgError('请选择查询时间')
        return
      }
      selectGpsOrbitList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        if (response.data.length <= 0) {
          this.msgError('未查询到该车辆的运行数据')
          return
        }
        let length = response.data.length
        let middle = -1
        if (length % 2 === 0) {
          middle = length / 2 + 1
        } else {
          middle = (length + 1) / 2
        }
        response.data.forEach(item => {
          let obj = {
            lng: item.longitude,
            lat: item.latitude
          }
          this.path.push(obj)
        })
        this.center = this.path[middle]
        this.startMark = this.path[0]
        this.endMark = this.path[this.path.length - 1]
        this.content = this.queryParams.vehicleLicence
        this.play = true
      }).catch(error => {

      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = [new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + ' 00:00:00', new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + ' 23:59:59']
    }
  },
  mounted() {
    listAllVehicle().then(response => {
      this.vehicleOptions = response.data
    })
  }
}
</script>
<style>

</style>

data关键参数说明:

play:truefalse,为true时,开始播放轨迹。
path:数组,格式为[{lng: 116.984646,lat: 36.659686}]
center:地图中心点,格式{lng: 116.984646,lat: 36.659686}
startMark:轨迹开始经纬度,格式{lng: 116.984646,lat: 36.659686}
endMark:轨迹终点经纬度,格式{lng: 116.984646,lat: 36.659686}
icon:轨迹图标,如下演示的小车图标
content:轨迹显示内容,如下按时的车牌
speed:轨迹绘制的速度,这里通过el-slider实现滑动调整速度

bm-polyline组件属性说明

path:绘制曲线的经纬度数组

bml-lushu

play:truefalse,为true时,开始播放轨迹。
infoWindow:truefalse,为true时,显示窗口,及content内容。

2

评论 (4)

取消
  1. 头像
    李森的博客
    MacOS · Safari

    我就是来看看

    回复
  2. 头像
    s
    Windows 10 · Google Chrome

    大佬请问 这两个接口api 数据是啥 可以麻烦发出来看一下吗 新手一个 selectGpsOrbitList listAllVehicle

    回复
    1. 头像
      Laughing 作者
      Windows 10 · Google Chrome
      @ s

      selectGpsOrbitList这个是获取轨迹的,坐标
      listAllVehicle这个是获取车辆信息的,左上角选择车辆那个地方

      回复
  3. 头像
    月儿
    Windows 10 · Google Chrome

    老师您好!我按照本方法操作了,再在线的情况下没有问题,离线后,其他功能都正常,只有bml-lushu不作用了,再离线情况下bml-lushu有什么要注意的点吗

    回复