AMap-Vue插件在Vue里实现一个高德地图车辆轨迹开发需求


效果图:

 根据插件文档安装一下AMap-Vue:

npm install --save @amap/amap-vue

按需引入:

// main.js
import AmapVueConfig from '@amap/amap-vue/lib/config';
AmapVueConfig.key = '开发者key';

直接贴index.vue代码:

<template>
  <div>
    <div class="amap-wrapper">
      <!-- 高德地图 -->
      <amap ref="myMap" :center="center" :zoom="zoom" @complete="mapLoading">
        <!-- 左上角旋转工具 -->
        <amap-tool-bar />
        <!-- 右下角缩放工具 -->
        <amap-control-bar
          :position="{
            top: '10px',
            left: '10px',
          }"
        />
        <!-- 起点标志位置 -->
        <amap-marker ref="myMarker1" :position="position1" />
        <amap-text
          :position="position1"
          :text="text1"
          :dom-style="{
            color: '#f00',
          }"
        />
        <!-- 终点标志 -->
        <amap-marker ref="myMarker2" :position="position2" />
        <amap-text
          :position="position2"
          :text="text2"
          :dom-style="{
            color: '#f00',
          }"
        />
        <!-- 轨迹 -->
        <amap-polyline :stroke-weight="8" :path="path" stroke-color="#f00" />
        <!-- 时间标志点 -->
        <amap-marker
          v-for="(itemkeyin locationDetails"
          :key="key"
          :icon="iconimg"
          :offset="[0, -10]"
          :position.sync="item.path"
          clickable
          @mouseover="showMouseover(item)"
        />
        <!-- 时间信息层 -->
        <amap-info-window
          :visible="!!coordinatesData"
          :position="coordinatesData ? coordinatesData.path : null"
          :offset="[10, -8]"
          auto-move
          is-custom
        >
          <div v-if="coordinatesData">
            <div v-show="coordinatesTo">
              <div class="parcel">
                <i class="el-icon-close" @click="coordinatesData = null"></i>
                <div class="text_li_css" style="padding-top: 30px">
                  时间:{{ coordinatesData.gtm }}
                </div>
                <div class="text_li_css">
                  经度:{{ coordinatesData.path[0] }}
                </div>
                <div class="text_li_css">
                  维度:{{ coordinatesData.path[1] }}
                </div>
              </div>
              <div class="mark_css"></div>
            </div>
          </div>
        </amap-info-window>
      </amap>
    </div>
  </div>
</template>
 
<script>
import iconimg from '@/assets/images/transparent.png';
import Amap from '@amap/amap-vue/lib/amap';
import AmapMarker from '@amap/amap-vue/lib/marker';
import AmapText from '@amap/amap-vue/lib/text';
import AmapPolyline from '@amap/amap-vue/lib/polyline';
import AmapControlBar from '@amap/amap-vue/lib/control-bar';
import AmapToolBar from '@amap/amap-vue/lib/tool-bar';
import AmapInfoWindow from '@amap/amap-vue/lib/info-window';
 
export default {
  components: {
    Amap,
    AmapMarker,
    AmapText,
    AmapPolyline,
    AmapControlBar,
    AmapToolBar,
    AmapInfoWindow
  },
  data () {
    return {
      //引入的时间标志图标
      iconimg,
      //时间标志位置数据
      locationDetails: [],
      //时间信息层坐标位置
      coordinatesData: { path: [116.39744739.909183], gtm: "" },
      //时间信息层初始隐藏
      coordinatesTo: false,
      //地图显示坐标位置
      center: [116.39744739.909183],
      //地图初始缩放等级
      zoom: 15,
      //起点标志位置
      position1: [00],
      text1: "起点位置",
      //终点标志位置
      position2: [00],
      text2: "终点位置",
      //轨迹坐标数据
      path: [],
      //异步网络请求的坐标数据
      locationData: {}
    }
  },
  methods: {
    //地图初始化调用
    mapLoading () {
      //发起网络请求接口返回坐标数据
     carriageTracesByTime({ vehicleId: 61 }).then((res=> {
              //数据格式res.data:[{"lon":"73963554","lat":"25073040","gtm":"20210916/170808"},...]
              this.locationData = res.data;
              //起点标志位置
              this.position1 = [this.locationData[0].lonthis.locationData[0].lat];
              //终点标志位置
              this.position2 = [this.locationData[this.locationData.length - 1].lonthis.locationData[this.locationData.length - 1].lat];
               //地图显示坐标位置
              this.center = [this.locationData[this.locationData.length - 1].lonthis.locationData[this.locationData.length - 1].lat];
              //轨迹坐标数据
              var path = [];
              //时间标志位置数据
              var locationDetails = [];
              for (let i of this.locationData) {
                path.push([i.loni.lat]);
                locationDetails.push({
                  path: [i.loni.lat],
                  gtm: i.gtm
                })
              }
              this.path = path;
              this.locationDetails = this.intervalMethods(locationDetails);
        });
    },
    //每隔60条就取一条时间坐标
    intervalMethods (data) {
      const mapData = data;
      var i = 0;
      const value = [];
      const mapMethods = () => {
        i += 60;
        if (i < mapData.length) {
          value.push(mapData[i]);
          mapMethods();
        } else {
          return
        }
      };
      mapMethods();
      return value;
    },
    //显示时间信息层
    showMouseover (data) {
      this.coordinatesTo = true;
      //时间信息层坐标位置
      this.coordinatesData = data;
    },
  }
}
</script>

 

<style scoped>
.amap-wrapper {
  width100%;
  height700px;
}
/* 信息层css */
.parcel {
  width220px;
  height120px;
  background-color#fff;
  border2px #3e94f9 solid;
  font-size14px;
  color#000;
  positionrelative;
  border-radius5px;
}
.el-icon-close {
  positionabsolute;
  top10px;
  right10px;
  font-size18px;
  cursorpointer;
}
.text_li_css {
  width90%;
  line-height20px;
  margin-left18px;
}
.mark_css {
  width0;
  height0;
  border-left10px solid transparent;
  border-right10px solid transparent;
  border-top14px solid #3e94f9;
  margin-left100px;
}
</style>
 

intervalMethods()方法主要是为了坐标点数据太多一次性渲染太多时间点位直接导致浏览器页面卡死未响应,如没有这方面问题和需求可以忽略掉这个方法。

 如果不需要时间标记信息层功能可以把 info-window 引入去掉和删掉时间标记相关代码。

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM