@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>

效果图:
折线

大数据线

当我们需要渲染数据量很庞大的线时,主要用于常见的高精地图、城市道路等,这时候可以使用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>

效果图:
loca线

当绘制高精地图的线出现显示不规则时需要在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 中使用 `el-amap` 组件手动绘制折线,可以通过引入其中的 `AmapManager` 组件来实现。以下是一个简单的实现方法: 1. 安装 `el-amap` 组件: ``` npm install el-amap --save ``` 2. 在 Vue 中引入 `AmapManager` 组件: ```vue <template> <el-amap class="map-container"> <el-amap-manager :plugin="plugin"></el-amap-manager> <el-button @click="drawPolyline">绘制折线</el-button> </el-amap> </template> <script> import { AmapManager } from 'el-amap'; export default { components: { AmapManager, }, data() { return { plugin: ['AMap.PolyEditor'], polyline: null, }; }, methods: { drawPolyline() { const editor = new AMap.PolyEditor(this.map, this.polyline); editor.open(); editor.on('end', () => { const path = this.polyline.getPath(); console.log('绘制完成', path); // 处理绘制结果 }); }, }, }; </script> ``` 3. 在 Vue 中创建地图实例和折线实例: ```vue <script> import { AmapManager } from 'el-amap'; export default { components: { AmapManager, }, data() { return { plugin: ['AMap.PolyEditor'], mapOptions: { center: [116.397428, 39.90923], zoom: 13, }, polylineOptions: { path: [], strokeColor: '#3366FF', strokeWeight: 5, }, polyline: null, }; }, mounted() { this.polyline = new AMap.Polyline(this.polylineOptions); this.polyline.setMap(this.$amap.getMap()); this.$amap.plugin('AMap.MouseTool', () => { const tool = new AMap.MouseTool(this.$amap.getMap()); tool.on('draw', (e) => { const path = e.obj.getPath(); console.log('绘制完成', path); // 处理绘制结果 this.polyline.setPath(path); this.polyline.setMap(this.$amap.getMap()); }); }); }, methods: { drawPolyline() { const tool = new AMap.MouseTool(this.$amap.getMap()); tool.polyline(); }, }, }; </script> ``` 以上是一个简单的实现方法,你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值