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