Vue 高德地图 js API Loca 如何使用 连接线图层、脉冲连线图层
阅读此文你需要已经了解并掌握的:
- 已经会使用常规地图的生成方式
- 已经了解如何载入
Loca插件
如果不了解,可以查看我之前的文章:
高德地图 Vue 中 加载 数据可视化 Loca 的方式
如何使用高德地图 API 做一个路线规划应用,展示自定义路线
高德地图 API Loca 3D动画的说明
最终实现的效果:
“感谢大哥送来的火箭”
线上实例:(这两个是一样的)
一、基础知识
1. 这个视图中包含四个图层:
- 一个盛放各省名称的文字图层
AMap.LabelsLayer - 一个盛放各省坐标点位置动画的图层
Loca.ScatterLayer - 一个盛放目标点坐标点位置的图层
Loca.ScatterLayer - 一个显示脉冲连接线的图层
Loca.PulseLinkLayer
2. 做一个这样的视图需要哪几个步骤:
- 新建一个
map,并载入Loca插件。 - 遍历所有省份数据,生成省份名称图层
AMap.LabelsLayer - 遍历所有省份数据,生成省份地理坐标标识图层
Loca.ScatterLayer - 生成目标点的标识图层
Loca.ScatterLayer - 遍历所有省份数据,每个数据包【含目标点】、【当前省份坐标点】两个坐标点的数据,根据两个地点数据生成一条脉冲连接线。然后生成所有省份的连接线
- 最终使 Loca 的动画动起来即可
3. 建立脉冲线需要了解的知识
脉冲线的建立过程是这样的:
// 建立图层 let pulseLayer = new Loca.PulseLinkLayer(图层参数) // 设置数据源 pulseLayer.setSource(数据参数) // 设置样式 pulseLayer.setStyle(样式参数)
讯享网
4. 用到的数据如何生成
Loca 图层接收的数据是 Loca.GeoJSONSource 格式的,而这个对象接收的内容格式是这样的
讯享网let locaLayerData = new Loca.GeoJSONSource({
data: {
'type': 'FeatureCollection', // 固定 'features': [ // 这里就是点的数组 {
'type': 'Feature', // 本例中我们用到的 geometry 有两个格式,一种是 `Point` 一种是 `LineString`, 'geometry': {
'type': 'Point', 'coordinates': [121., 25.046711], // 地理经纬度 }, }, ], }, })
这是 LineString 格式时的 geometry 结构,
'geometry': {
'type': 'LineString ', 'coordinates': [ // 地理经纬度,这里有两个经纬度值 [121., 25.046711], // 连接线的起点 [121., 25.046711] // 连接线的终点 ], },

讯享网[ {
"name": "北京市", "center": "116.,39." }, {
"name": "天津市", "center": "117.,39.084158" }, ]
我们就可以根据这个生成点【坐标层图层数据】:
computed: {
// 根据省份地址,生成展示地图需要的格式化数据 dataPoints(){
let tempData = GEO_PROVINCE_DATA.map(item => {
let co = item.center.split(',').map(item => Number(item)) // 将字符串拆分成坐标数组数据 return {
"type": "Feature", "properties": {
"province": item.name}, "geometry": {
"type": "Point", // 点位 "coordinates": co } } }) return {
"type": "FeatureCollection", "features": tempData } }, // 连接线图层数据 dataLines(){
let tempData = GEO_PROVINCE_DATA.map(item => {
let co = item.center.split(',').map(item => Number(item)) // 将字符串拆分成坐标数组数据 return {
"type": "Feature", "properties": {
"province": item.name}, "geometry": {
"type": "LineString", // 线段 "coordinates": [ TARGET_POINT, // target location co ] } } }) return {
"type": "FeatureCollection", "features": tempData } }, }
使用的时候生成 geo 数据
讯享网const geoDataPoints = new Loca.GeoJSONSource({
data: this.dataPoints, }) const geoDataLines = new Loca.GeoJSONSource({
data: this.dataLines, })
根据这些数据载入图层:
// 图层点坐标 let loadLocation = () => {
setLabelsLayer(this.dataPoints) scatterLayer2.setSource(geoDataPoints) scatterLayer2.setStyle({
size: [, ], // 点的大小 unit: 'miter', animate: true, duration: 1000, texture: 'https://a.amap.com/Loca/static/static/orange.png', // texture: 'https://a.amap.com/Loca/static/static/green.png', }) this.loca.add(scatterLayer2) // this.loca.animate.start() // 开始动画 } loadLocation()
讯享网// 连接线图层 let linkLayer = new Loca.LinkLayer({
zIndex: 20, opacity: 1, visible: true, zooms: [2, 22], }) let loadLine = () => {
linkLayer.setSource(geoDataLines) linkLayer.setStyle({
lineColors: ['#ff7514', '#ff0008'], height: (index, item) => {
return item.distance / 2 }, smoothSteps: 300 }) this.loca.add(linkLayer) } loadLine()
// pulse layer let pulseLayer = new Loca.PulseLinkLayer({
zIndex: 20, opacity: 1, visible: true, zooms: [2, 22], }) let loadPulse = () => {
pulseLayer.setSource(geoDataLinesReverse) pulseLayer.setStyle({
height: (index, item) => {
return item.distance / 2 }, unit: 'meter', dash: [40000, 0, 40000, 0], lineWidth: function () {
return [20000, 2000]; // 始末 节点的线段宽度 }, // altitude: 1000, smoothSteps: 100, // 曲线圆滑度 speed: function (index, prop) {
return 1000 + Math.random() * ; }, flowLength: , lineColors: function (index, feat) {
return ['rgb(255,221,0)', 'rgb(255,141,27)', 'rgb(65,0,255)']; }, maxHeightScale: 0.3, // 弧顶位置比例 headColor: 'rgba(255, 255, 0, 1)', // 线段中流动的 trailColor: 'rgb(255,84,84)', }) this.loca.add(pulseLayer) } loadPulse()
这些结束后,让动画动起来
讯享网this.map.on('complete', ()=> {
this.loca.animate.start() })
此时俯视图是这个样子:

再加一些 Loca 动画,就成了这个样子

备注:用到的官方数据、资源
各省会数据: https://a.amap.com/Loca/static/mock/districts.js

中心点图标: https://a.amap.com/Loca/static/static/center-point.png
动画点(绿色):https://a.amap.com/Loca/static/static/green.png

动画点(橙色):https://a.amap.com/Loca/static/static/green.png

完整代码:自行将 appID 替换成自己的
province.json
[ {
"name": "北京市", "center": "116.,39." }, {
"name": "天津市", "center": "117.,39.084158" }, {
"name": "河北省", "center": "114.,38.037433" }, {
"name": "山西省", "center": "112.,37." }, {
"name": "内蒙古自治区", "center": "111.76629,40.81739" }, {
"name": "辽宁省", "center": "123.,41." }, {
"name": "吉林省", "center": "125.32568,43." }, {
"name": "黑龙江省", "center": "126.,45." }, {
"name": "上海市", "center": "121.,31." }, {
"name": "江苏省", "center": "118.,32.060875" }, {
"name": "浙江省", "center": "120.,30." }, {
"name": "安徽省", "center": "117.,31." }, {
"name": "福建省", "center": "119.,26." }, {
"name": "江西省", "center": "115.81635,28.63666" }, {
"name": "山东省", "center": "117.019915,36." }, {
"name": "河南省", "center": "113.,34." }, {
"name": "湖北省", "center": "114.,30." }, {
"name": "湖南省", "center": "112.9836,28." }, {
"name": "广东省", "center": "113.26641,23." }, {
"name": "广西壮族自治区", "center": "108.,22." }, {
"name": "海南省", "center": "110.,20.017377" }, {
"name": "重庆市", "center": "106.,29." }, {
"name": "四川省", "center": "104.075809,30." }, {
"name": "贵州省", "center": "106.70546,26." }, {
"name": "云南省", "center": "102.,25.045806" }, {
"name": "西藏自治区", "center": "91.,29." }, {
"name": "陕西省", "center": "108.,34." }, {
"name": "甘肃省", "center": "103.,36.05956" }, {
"name": "青海省", "center": "101.,36." }, {
"name": "宁夏回族自治区", "center": "106.,38." }, {
"name": "新疆维吾尔自治区", "center": "87.,43." }, {
"name": "香港特别行政区", "center": "114.,22." }, {
"name": "澳门特别行政区", "center": "113.,22." } ]
MapLoca.vue
讯享网<template> <div class="map-container"> <div id="container" :style="`height: ${insets.height}px`"></div> </div> </template> <script> import AMapLoader from '@amap/amap-jsapi-loader' import {
mapState} from "vuex" import GEO_PROVINCE_DATA from './province.json' import LaunchButton from "@/components/LaunnchButton"; let AMap = null const TARGET_POINT = [121., 25.046711] // 目标坐标 台湾 const DESTINATION_POINT = [110., 28.046711] // 目标坐标 export default {
name: "MapLoca", components: {
LaunchButton}, data() {
return {
isLoading: false, contentHeight: 400, map: null, loca: null, } }, mounted() {
this.contentHeight = window.innerHeight AMapLoader.load({
key: "替换成自己申请的高德web app id", // 开发应用的 ID version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15 plugins: [], Loca:{
version: '2.0.0', }, AMapUI: {
// 是否加载 AMapUI,缺省不加载 version: '1.1', // AMapUI 缺省 1.1 plugins: [], // 需要加载的 AMapUI ui插件 }, }).then(map => {
AMap = map this.map = new AMap.Map('container', {
viewMode: '3D', zoom: 6, pitch: 32, center: TARGET_POINT, mapStyle: 'amap://styles/grey', showBuildingBlock: true, // 显示建筑物 showLabel: false, // 不显示地名什么的 }) // 文字图层 let labelLayer = new AMap.LabelsLayer({
rejectMapMask: true, collision: true, animation: true, }) this.map.add(labelLayer) this.loca = new Loca.Container({
map: this.map, }) let scatterLayer2 = new Loca.ScatterLayer({
zIndex: 10, opacity: 0.8, visible: true, zooms: [2, 22], }) let scatterLayer3 = new Loca.ScatterLayer({
zIndex: 10, opacity: 0.8, visible: true, zooms: [2, 22], }) let centerPoint = new Loca.GeoJSONSource({
data: {
'type': 'FeatureCollection', 'features': [ {
'type': 'Feature', 'geometry': {
'type': 'Point', 'coordinates': TARGET_POINT, }, }, ], }, }) scatterLayer3.setSource(centerPoint) scatterLayer3.setStyle({
size: [, ], unit: 'meter', texture: 'https://a.amap.com/Loca/static/static/center-point.png', }) this.loca.add(scatterLayer3) let lineGeoMap let scatterGeoMap let setLabelsLayer = (data) => {
labelLayer.clear() data.features.forEach((item) => {
let labelsMarker = new AMap.LabelMarker({
name: item.properties.province, position: item.geometry.coordinates, zooms: [2, 22], opacity: 1, zIndex: 10, text: {
content: item.properties.province, direction: 'bottom', offset: [0, -5], style: {
fontSize: 13, fontWeight: 'normal', fillColor: '#fff', }, }, }) labelLayer.add(labelsMarker) }) labelLayer.add( new AMap.LabelMarker({
name: '台湾', position: TARGET_POINT, zooms: [2, 22], opacity: 1, zIndex: 10, rank: 100, text: {
content: '台湾', direction: 'bottom', offset: [0, -5], style: {
fontSize: 13, fontWeight: 'normal', fillColor: '#fff', }, }, }), ) } const geoDataPoints = new Loca.GeoJSONSource({
data: this.dataPoints, }); const geoDataLines = new Loca.GeoJSONSource({
data: this.dataLines, }); const geoDataLinesReverse = new Loca.GeoJSONSource({
data: this.dataLinesReverse, }); let loadLocation = () => {
setLabelsLayer(this.dataPoints) scatterLayer2.setSource(geoDataPoints) scatterLayer2.setStyle({
size: [, ], unit: 'miter', animate: true, duration: 1000, texture: 'https://a.amap.com/Loca/static/static/orange.png', // texture: 'https://a.amap.com/Loca/static/static/green.png', }) this.loca.add(scatterLayer2) // this.loca.animate.start() // 开始动画 } loadLocation() let linkLayer = new Loca.LinkLayer({
zIndex: 20, opacity: 1, visible: true, zooms: [2, 22], }) let loadLine = () => {
linkLayer.setSource(geoDataLines) linkLayer.setStyle({
lineColors: ['#ff7514', '#ff0008'], height: (index, item) => {
return item.distance / 2 }, smoothSteps: 300 }) this.loca.add(linkLayer) } // loadLine() // pulse layer let pulseLayer = new Loca.PulseLinkLayer({
zIndex: 20, opacity: 1, visible: true, zooms: [2, 22], }) let loadPulse = () => {
pulseLayer.setSource(geoDataLinesReverse) pulseLayer.setStyle({
height: (index, item) => {
return item.distance / 2 }, unit: 'meter', dash: [40000, 0, 40000, 0], lineWidth: function () {
return [20000, 2000]; // 始末 节点的线段宽度 }, // altitude: 1000, smoothSteps: 100, // 曲线圆滑度 speed: function (index, prop) {
return 1000 + Math.random() * ; }, flowLength: , lineColors: function (index, feat) {
return ['rgb(255,221,0)', 'rgb(255,141,27)', 'rgb(65,0,255)']; }, maxHeightScale: 0.3, // 弧顶位置比例 headColor: 'rgba(255, 255, 0, 1)', trailColor: 'rgb(255,84,84)', }) this.loca.add(pulseLayer) } loadPulse() this.animateStart() this.map.on('complete', ()=> {
this.loca.animate.start() }) }).catch(e => {
console.log(e) }) }, computed: {
...mapState(['insets']), // 根据省份地址,生成展示地图需要的格式化数据 dataPoints(){
let tempData = GEO_PROVINCE_DATA.map(item => {
let co = item.center.split(',').map(item => Number(item)) return {
"type": "Feature", "properties": {
"province": item.name}, "geometry": {
"type": "Point", // 点位 "coordinates": co } } }) return {
"type": "FeatureCollection", "features": tempData } }, dataLines(){
let tempData = GEO_PROVINCE_DATA.map(item => {
let co = item.center.split(',').map(item => Number(item)) return {
"type": "Feature", "properties": {
"province": item.name}, "geometry": {
"type": "LineString", // 线段 "coordinates": [ TARGET_POINT, // target location co ] } } }) return {
"type": "FeatureCollection", "features": tempData } }, dataLinesReverse(){
let tempData = GEO_PROVINCE_DATA.map(item => {
let co = item.center.split(',').map(item => Number(item)) return {
"type": "Feature", "properties": {
"province": item.name}, "geometry": {
"type": "LineString", // 线段 "coordinates": [ co, TARGET_POINT // target location ] } } }) return {
"type": "FeatureCollection", "features": tempData } }, }, methods: {
animateStart(){
this.loca.viewControl.addAnimates([{
center: {
value: DESTINATION_POINT, // 动画终点的经纬度 control: [TARGET_POINT, DESTINATION_POINT], // 过渡中的轨迹控制点,地图上的经纬度 timing: [0.42, 0, 0.4, 1], // 动画时间控制点 duration: 5000, // 过渡时间,毫秒(ms) }, // 俯仰角动画 pitch: {
value: 60, // 动画终点的俯仰角度 control: [[0, 0], [1, 60]], // 控制器,x是0~1的起始区间,y是pitch值 timing: [0, 0, 1, 1], // 这个值是线性过渡 duration: 5000, }, // 缩放等级动画 zoom: {
value: 5, // 动画终点的地图缩放等级 control: [[0, 8], [1, 5]], // 控制器,x是0~1的起始区间,y是zoom值 timing: [0, 0, 1, 1], duration: 8000, }, // 旋转动画 rotation: {
value: -30, // 动画终点的地图旋转角度 control: [[0, 0], [1, -30]], // 控制器,x是0~1的起始区间,y是rotation值 timing: [0, 0, 1, 1], duration: 8000, } }], () => {
}) }, resizeMap() {
let mapContainer = document.getElementById('container') mapContainer.style.height = window.innerHeight + "px" mapContainer.style.width = window.innerWidth + "px" }, }, beforeUnmount() {
this.loca.destroy() // 需要先销毁 Loca 再销毁 Map this.map.destroy() // 销毁地图,释放内存 this.map = null } } </script> <style lang="scss" scoped> .map-container {
position: relative; } </style>


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/82534.html