高德地图画线

<template>
  <!-- <el-card> -->
  <div class="card">
    <div class="query-from">
      <el-form :model="dataForm">
        <el-form-item label="车辆VIN:">
          <el-input v-model="dataForm.vin" style="width: 220px" placeholder="单行输入" />
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-button type="primary" @click="getqueryTrack()">查询</el-button>
    </div>
  </div>
  <!-- </el-card> -->
  <div class="map-content">
    <div id="container"></div>
  </div>
</template>
<script>
import { defineComponent, onBeforeMount, reactive, toRefs } from "vue";
import { shallowRef } from "@vue/reactivity";
import AMapLoader from "@amap/amap-jsapi-loader";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
export default defineComponent({
  setup() {
    const map = shallowRef(null);
    const state = reactive({
      dataForm: {
        vin: "" //车辆vin
      },
      // 组件加载后隐藏
      showInfoWindow: false,
      infoWindow: {},
      trackInfoList: [], //过滤后deviceInfo === 0 的值
      filtrackInfoList: [], //过滤后经纬度的值
      dataList: []
    });
    //#region
    // onBeforeMount(() => {
    //   initMap();
    // });
    // // 初始化地图
    // const initMap = () => {
    //   AMapLoader.load({
    //     key: "725997161d56e28f560d01da79a013a1", // 申请好的Web端开发者Key,首次调用 load 时必填
    //     version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
    //     plugins: [""] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
    //   })
    //     .then((AMap) => {
    //       this.map = new AMap.Map("container", {
    //         //设置地图容器id
    //         viewMode: "2D", //是否为3D地图模式
    //         zoom: 5, //初始化地图级别
    //         center: [105.602725, 37.076636] //初始化地图中心点位置
    //       });
    //       initLine();
    //       console.log(12312312);
    //     })
    //     .catch((e) => {
    //       console.log(e);
    //     });
    // };

    // // 地图轨迹点
    // const initLine = () => {
    //   var path = [
    //     [116.362209, 39.887487],
    //     [116.422897, 39.878002],
    //     [116.372105, 39.90651],
    //     [116.428945, 39.89663]
    //   ];
    //   var polyline = new AMap.Polyline({
    //     path: path,
    //     isOutline: true,
    //     outlineColor: "#ffeeff",
    //     borderWeight: 3,
    //     strokeColor: "#3366FF",
    //     strokeOpacity: 1,
    //     strokeWeight: 6,
    //     // 折线样式还支持 'dashed'
    //     strokeStyle: "solid",
    //     // strokeStyle是dashed时有效
    //     strokeDasharray: [10, 5],
    //     lineJoin: "round",
    //     lineCap: "round",
    //     zIndex: 50
    //   });

    //   map.add([polyline]);
    //   map.setFitView();
    // }
    //initMap,initLine
    //#endregion
    return { ...toRefs(state), map };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    // 初始化地图
    initMap() {
      AMapLoader.load({
        key: "725997161d56e28f560d01da79a013a1", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [""] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.map = new AMap.Map("container", {
            //设置地图容器id
            viewMode: "2D", //是否为2D地图模式
            zoom: 5, //初始化地图级别 地图初始放大级别 0 - 22
            center: [105.602725, 37.076636] //初始化地图中心点位置
          });
          // this.getqueryTrack()
          this.initLine();
        })
        .catch((e) => {
          console.log(e);
        });
    },
    // 地图轨迹点
    initLine(res) {
      console.log("path", res);
      // var path = res;
      var path = [
        [116.362209, 39.887487],
        [116.422897, 39.878002],
        [116.372105, 39.90651],
        [116.428945, 39.89663]
      ];
      let path5 = [
        { lng: 116.362209, lat: 39.887487 },
        { lng: 116.422897, lat: 39.878002 },
        { lng: 116.372105, lat: 39.90651 },
        { lng: 116.428945, lat: 39.89663 }
      ];
      var polyline = new AMap.Polyline({
        path: path,
        isOutline: true,
        outlineColor: "#ffeeff",
        borderWeight: 3,
        strokeColor: "#4B679E",
        strokeOpacity: 1,
        strokeWeight: 6,
        lineJoin: "round", //折线拐点
        lineCap: "square", //折线两端绘制样式
        // 折线样式还支持 'dashed'
        strokeStyle: "solid",
        //轮廓线宽度
        // strokeWeight: "2",
        // strokeStyle是dashed时有效
        strokeDasharray: [10, 5],
        zIndex: 50
      });

      this.map.add([polyline]);
      this.map.setFitView();
      this.initMaker(path);
    },

    // LVYPDBZD1LP166524

    //查询物流轨迹信息
    getqueryTrack() {
      baseService.get("/sys/gpstaskinfo/vin/" + this.dataForm.vin).then((res) => {
        console.log("path1", res);
        this.dataList = res.data.gpsPositioningInfoDTOList;
        if (res.code !== 0) {
          return ElMessage.error(res.msg);
        }

        //#region
        // for(let item = 0; item <= res.data.gpsPositioningInfoDTOList.length; item++){
        //   if(item.deviceInfo === 0){
        //     this.trackInfoList.push(item)
        //   }
        // }
        //#endregion

        // 过滤deviceInfo是否等于0
        let trackInfoList = [];
        res.data.gpsPositioningInfoDTOList.forEach((item, i) => {
          if (item.deviceInfo === 0) {
            trackInfoList.push(item);
          }
        });

        //提取坐标点
        let filtrackInfoList = [];
        trackInfoList.forEach((item1, i) => {
          filtrackInfoList.push(item1.lng, item1.lat);
        });

        this.initLine(this.arrToTwoDim(filtrackInfoList));

        // this.initMaker(trackInfoList);
      });
    },

    //一维数组处理成二维数组
    arrToTwoDim(arr, n = 2) {
      var arr2 = [];
      for (var i = 0, j = 0; i < arr.length; i += n) {
        // 初始化数组单个元素为数组
        arr2[j] = [];
        for (var k = 0; k < n; k++) {
          if (i + k < arr.length) arr2[j].push(arr[i + k]);
        }
        j++;
      }
      return (this.filtrackInfoList = arr2);
    },

    // 地图标点
    initMaker(e) {
      if (e) {
        var pos_icon = [];
        var pos_marker = [];
        for (let i in e) {
          pos_icon[i] = new AMap.Icon({
            // 图标尺寸
            size: new AMap.Size(12, 12),
            // 图标的取图地址
            image: "https://shanci.oss-cn-hangzhou.aliyuncs.com/fuhong/20220920/672170add10a4bddaa92c98a70b65e7c.png",
            // 图标所用图片大小
            imageSize: new AMap.Size(12, 12)
          });
          // console.log("ssss", icons);
          pos_marker[i] = new AMap.Marker({
            position: e[i],
            icon: pos_icon[i],
            anchor: "center", //设置锚点
            offset: new AMap.Pixel(0, 0) //设置偏移量
          });

          this.map.add(pos_marker[i]);
        }
        // for (let j in e.splice(-1)) {
        //   var marker = new AMap.Marker({
        //     position: new AMap.LngLat(e[j].lng, e[j].lat),
        //     offset: new AMap.Pixel(-10, -10),
        //     icon: "//webapi.amap.com/theme/v1.3/images/newpc/way_btn2.png", // 添加 Icon 图标 URL
        //     title: "北京"
        //   });
        //   console.log(11111);
        //   //添加marker标记
        //   var marker = new AMap.Marker({
        //     map: this.map,
        //     position: [e[j].lng, e[j].lat]
        //   });
        //   console.log(22222);
        //   //鼠标点击marker弹出自定义的信息窗体
        //   // marker.on("click", function () {
        //   //   infoWindow.open(map, marker.getPosition());
        //   // });
        //   marker.on("click", this.markerClick);
        //   this.map.add(marker);
        //   console.log(33333);
        // }
      }
    },
    //添加marker标记
    // addMarker() {
    //   map.clearMap();
    //   var marker = new AMap.Marker({
    //     map: map,
    //     position: [116.481181, 39.989792]
    //   });
    //   //鼠标点击marker弹出自定义的信息窗体
    //   marker.on("click", function () {
    //     infoWindow.open(map, marker.getPosition());
    //   });
    // }

    //#region
    // addMarker() {
    //   console.log("123123123123123123123123123123");
    //   // 创建标记点Marker
    //   let labelMarker = new AMap.LabelMarker({
    //     icon: "icon路径",
    //     position: "position定位点"
    //   });
    //   // labelMarker都绑定上点击事件
    //   labelMarker.on("click", (e) => {
    //     // 点击后创建自定义信息窗口
    //     this.setInfoWindows(title, content, e);
    //     // 把地图中心点设置为当前点击的标记点
    //     this.map.setZoomAndCenter(this.zoom, item.position);
    //   });
    // },
    // setInfoWindows(title, content, e) {
    //   console.log(e);
    //   // 此时需要把组件的样式设置为可见
    //   this.showInfoWindow = true;
    //   // 设置marker头部的标题信息窗口
    //   const infoWindow = new this.AMap.InfoWindow({
    //     // 使用自定义窗体
    //     isCustom: true,
    //     // 只有当组件渲染完毕后,通过$el才能拿到原生的dom对象
    //     content: this.$refs["infoWindow"].$el,
    //     // 设置定位偏移量
    //     offset: new this.AMap.Pixel(-9, -60)
    //   });
    //   this.infoWindow = infoWindow;
    //   // 信息窗口打开
    //   infoWindow.open(this.map, e.target.getPosition());
    // }
    //#endregion

    // 点标记点击事件
    markerClick(e) {
      console.log(e);
      this.infoWindow = new AMap.InfoWindow({
        offset: new AMap.Pixel(0, -35),
        content: this.content(e.target.getExtData()) // 覆盖物自定义extData
      });
      this.infoWindow.open(this.map, e.target.getPosition()); // 在地图的指定位置打开信息窗体

      // 在信息弹窗内添加交互事件
      // 因为添加到infowindow中的button绑定的是window的全局方法,所以这里需要将组件的局部方法赋值给全局方法。
      if (!window.updateUser) {
        window.updateUser = this.updateUser;
      }
    },

    // 信息弹窗 content,用的是拼接字符串的方法
    content(val) {
      var tips = val.tips;
      var v = this.dataList;
      var content = `
      <div>
	      <div class="wuliu">物流信息</div>
        <div class="map_tips_user">
        	<div class="user_list_one">
        		<span>车辆vin:</span>
        		<span class="user_list_text">${v[0].carId}</span>
        	</div>
        	<div class="user_list_one">
        		<span>更新时间:</span>
        		<span class="user_list_text" >${v[0].createDate}</span>
        	</div>
        	<div class="user_list_one">
        		<span>位置:</span>
        		<span class="user_list_text">--</span>
        	</div>
        	<div class="user_list_one">
        		<span>车辆状态:</span>
        		<span class="user_list_text">待运输整备</span>
        	</div>
       	</div>
      </div>`;
      return content;
    }
  }
});
</script>

<style lang="less" scoped>
#container {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 800px;
}
.card {
  display: flex;
  .query-from {
    padding-right: 15px;
  }
}
.map-content {
  width: 94.5vw;
  height: 100vh;
  margin-bottom: 0px;
}
.wuliu {
  background-color: antiquewhite;
  height: 30px;
  line-height: 30px;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建用户界面。而高德地图是一种基于Web的地图服务,提供了丰富的地图展示和地理信息处理功能。在Vue中使用高德地图画线可以通过以下步骤实现: 1. 首先,在Vue项目中引入高德地图JavaScript API。可以通过在HTML文件中添加`<script>`标签引入高德地图的API,或者使用npm安装相应的包。 2. 在Vue组件中创建一个地图容器,可以使用`<div>`标签来创建一个具有唯一ID的容器。 3. 在Vue组件的`mounted`生命周期钩子函数中初始化地图对象。可以使用`new AMap.Map()`来创建一个地图对象,并将其绑定到之前创建的容器上。 4. 使用高德地图提供的API来绘制线条。可以使用`AMap.Polyline`类来创建一个折线对象,并设置其路径、样式等属性。然后将折线对象添加到地图上,即可在地图上显示出线条。 下面是一个简单的示例代码: ```vue <template> <div id="mapContainer"></div> </template> <script> export default { mounted() { // 初始化地图对象 const map = new AMap.Map('mapContainer', { zoom: 13, center: [116.397428, 39.90923] }); // 创建折线对象 const polyline = new AMap.Polyline({ path: [ [116.368904, 39.913423], [116.382122, 39.901176], [116.387271, 39.912501], [116.398258, 39.904600] ], strokeColor: '#FF0000', strokeWeight: 6 }); // 将折线对象添加到地图上 map.add(polyline); } } </script> ``` 这样,当该Vue组件被渲染时,就会在地图容器中显示出一条红色的折线
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值