@vuemap/vue-amap高德vue组件库常用技巧(八)- 线

@vuemap/vue-amap是基于高德JSAPI2.0、Loca2.0封装的vue组件库,支持vue2、vue3版本。首页地址:https://vue-amap.guyixi.cn/

在上一个分享中,主要讲解了如何在地图上通过鼠标组件实现电子围栏的绘制。这一次主要讲解怎么使用地图提供的各种线组件。

组件库中对于线的展示提供了几个不同的组件,有基础的矢量线、适合大数据量的loca的线、动态线、连接线等等,下面对于各个场景下线的使用进行详细说明。

普通线

当我们只需要展示普通的线,比如几百个点的折线,同时也可能会需要编辑线,这时候可以使用el-amap-polyline进行展示,该组件支持普通的实线、折线等样式,也可以开启编辑模式编辑线,线的path数据支持双向绑定。

示例代码如下:

<template>
  <div class="map-page-container">
    <el-amap
      :show-label="false"
      :center="center"
      :zoom="zoom"
      @click="clickMap"
      @init="initMap"
    >
      <el-amap-polyline
        :editable="polyline.editable"
        :visible="polyline.visible"
        :draggable="polyline.draggable"
        :path="polyline.path"
        @click="click"
      />
    el-amap>
  div>
  <div class="toolbar">
    <button @click="toggleVisible">
      {{ polyline.visible ? '隐藏标记' : '显示标记' }}
    button>
    <button @click="changeDraggable">
      {{ polyline.draggable ? '禁止标记移动' : '允许标记移动' }}
    button>
    <button @click="changeEditable">
      {{ polyline.editable ? '停止编辑' : '开始编辑' }}
    button>
  div>
template>

<script lang="ts" setup>
import {ref, reactive} from "vue";
import {ElAmap, ElAmapPolyline} from "@vuemap/vue-amap";

const zoom = ref(15);
const center = ref([121.5273285, 31.25515044]);
const polyline = reactive({
  path: [[121.5389385, 31.21515044], [121.5389385, 31.29615044], [121.5273285, 31.21515044]],
  editable: false,
  visible: true,
  draggable: false
});
const clickMap = (e) => {
  console.log('click map: ', e);
}
const initMap = (map) => {
  console.log('init map: ', map);
}
const toggleVisible = () => {
  polyline.visible = !polyline.visible;
}
const changeEditable = () => {
  polyline.editable = !polyline.editable;
}
const changeDraggable = () => {
  polyline.draggable = !polyline.draggable;
}
const click = (e) => {
  alert('click GeoJSON');
}
script>

<style scoped>
style>

效果图:
@vuemap/vue-amap高德vue组件库常用技巧(八)- 线_第1张图片

大数据线

当我们需要渲染数据量很庞大的线时,主要用于常见的高精地图、城市道路等,这时候可以使用loca提供的线图层,该图层的线除了支持经纬度数据,还支持高程数据。

示例代码如下:

<template>
  <div class="map-page-container">
    <el-amap
      view-mode="3D"
      :pitch="pitch"
      :show-label="false"
      :center="center"
      :zoom="zoom"
      @click="clickMap"
      @init="initMap"
    >
      <el-amap-loca>
        <el-amap-loca-line
          :visible="visible"
          :source-url="sourceUrl"
          :layer-style="layerStyle"
        />
      el-amap-loca>
    el-amap>
  div>
  <div class="toolbar">
    <button @click="changeVisible">
      {{ visible ? '隐藏' : '显示' }}
    button>
  div>
template>

<script lang="ts" setup>
import {ref} from "vue";
import {ElAmap} from "@vuemap/vue-amap";
import {ElAmapLoca, ElAmapLocaLine} from "@vuemap/vue-amap-loca";

const zoom = ref(8);
const center = ref([116.335036, 39.900082]);
const pitch = ref(55)

const sourceUrl = ref('https://a.amap.com/Loca/static/loca-v2/demos/mock_data/bj_bus.json');
const colors = ['#f7fcf5', '#e5f5e0', '#c7e9c0', '#a1d99b', '#74c476', '#41ab5d', '#238b45', '#006d2c', '#00441b'].reverse();
const layerStyle = ref({
  color (index, prop) {
    const i = index % colors.length;
    return colors[i];
  },
  lineWidth: (index, prop) => {
    const i = index % colors.length;
    return i * 0.1 + 2;
  },
  altitude (index, feature) {
    const i = index % colors.length;
    return 100 * i;
  },
  // dashArray: [10, 5, 10, 0],
  dashArray: [10, 0, 10, 0],
})


const visible = ref(true)
const changeVisible = () => {
  visible.value = !visible.value;
}

const clickMap = (e) => {
  console.log('click map: ', e);
}
const initMap = (map) => {
  console.log('init map: ', map);
}
script>

<style scoped>
style>

效果图:
@vuemap/vue-amap高德vue组件库常用技巧(八)- 线_第2张图片

当绘制高精地图的线出现显示不规则时需要在init事件中调用setCustomCenter方法,将中心点移到面数据的某个点上

initPolyline(lineLayer){
  lineLayer.setCustomCenter(this.lines.features[0].geometry.coordinates[0][0]);
}

轨迹线

轨迹线是用于显示一条线路的行进轨迹,普通的线没有所谓的起点终点的划分,当我们需要通过一定的直观的效果显示整个轨迹的方向,那么就可以使用轨迹线。

示例代码如下:

<template>
  <div class="map-page-container">
    <el-amap
      view-mode="3D"
      map-style="amap://styles/dark"
      :pitch="pitch"
      :show-label="false"
      :center="center"
      :zoom="zoom"
      @click="clickMap"
      @init="initMap"
    >
      <el-amap-loca @init="initLoca">
        <el-amap-loca-pulse-line
          :visible="visible"
          :source-url="sourceUrl"
          :layer-style="layerStyle"
          :visible-duration="500"
        />
      el-amap-loca>
    el-amap>
  div>
  <div class="toolbar">
    <button @click="changeVisible">
      {{ visible ? '隐藏' : '显示' }}
    button>
  div>
template>

<script lang="ts" setup>
import {ref} from "vue";
import {ElAmap} from "@vuemap/vue-amap";
import {ElAmapLoca, ElAmapLocaPulseLine} from "@vuemap/vue-amap-loca";

const zoom = ref(12);
const center = ref([116.352734,39.8447]);
const pitch = ref(55)

const sourceUrl = ref('https://a.amap.com/Loca/static/loca-v2/demos/mock_data/bj_bus.json');
const headColors = ['#EFBB51', '#7F3CFF', '#4CC19B', '#0B5D74', '#E06AC4', '#223F9B', '#F15C1A', '#7A0FA6'];
const layerStyle = ref({
                altitude: 0,
                lineWidth: 2,
                // 脉冲头颜色
                headColor: (_, feature) => {
                    return headColors[feature.properties.type - 1];
                },
                // 脉冲尾颜色
                trailColor: 'rgba(128, 128, 128, 0.5)',
                // 脉冲长度,0.25 表示一段脉冲占整条路的 1/4
                interval: 0.25,
                // 脉冲线的速度,几秒钟跑完整段路
                duration: 5000,
            })


const visible = ref(true)
const changeVisible = () => {
  visible.value = !visible.value;
}

const clickMap = (e) => {
  console.log('click map: ', e);
}
const initMap = (map) => {
  console.log('init map: ', map);
}

const initLoca = (loca: any) => {
  loca.animate.start();
}

script>

<style scoped>
style>

效果图:

你可能感兴趣的:(vue.js,javascript,数据可视化,前端,3d)