如何利用ArcGIS JS实现动态地图动画效果?

小贝
预计阅读时长 18 分钟
位置: 首页 公众号 正文

ArcGIS JS 动画

arcgis js 动画

ArcGIS JavaScript API 提供了丰富的动画功能,可以用于在地图上展示动态数据和效果,本文将详细介绍如何使用 ArcGIS JS 实现几种常见的动画效果,包括动态脉冲效果和车辆轨迹动画。

一、ArcGIS JS 简介

ArcGIS JavaScript API 是由 Esri 公司提供的用于在 Web 应用程序中使用 ArcGIS 地图和地理空间数据的 JavaScript API,它提供了一组工具,用于创建丰富的地图应用程序,包括地图显示、图层控制、查询和分析等功能。

二、动态脉冲效果

1. 实现步骤

动态脉冲效果通过定时刷新,每次脉冲渲染圈不断放大,并且透明度逐渐缩小,直到达到一定大小后再退回0,以下是具体的实现步骤:

(1)引入必要的模块

import MapView from "@arcgis/core/views/MapView";
import GraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
import Point from "@arcgis/core/geometry/Point";
import SimpleMarkerSymbol from "@arcgis/core/symbols/SimpleMarkerSymbol";
import Graphic from "@arcgis/core/Graphic";
import SceneView from "@arcgis/core/views/SceneView";

(2)定义 FlashPointLayer 类

class FlashPointLayer {
    constructor(view) {
        this.view = view;
        this.flashLayer = new GraphicsLayer({ id: 'flashLayer', title: 'flashLayer' });
        this.piontArr = [];
        this.markerSymbol = new SimpleMarkerSymbol({
            style: 'circle',
            size: 15,
            color: [255, 0, 0, 0.85],
            outline: { color: [0, 0, 0, 0] }
        });
        this.size = 5;
        this.alpha = (101 this.size) / 100;
        this.animationId = 0;
        this.startAnimation();
    }

(3)添加多个闪烁点

arcgis js 动画
addManyPoint(pointArr) {
    pointArr.forEach(point => {
        this.piontArr.push(new Point({
            x: point[0],
            y: point[1],
            spatialReference: this.view.spatialReference,
        }));
    });
}

(4)添加单个闪烁点

addPoint(lon, lat) {
    const point = new Point({ x: lon, y: lat, spatialReference: this.view.spatialReference });
    this.piontArr.push(point);
}

(5)启动动画

startAnimation() {
    const centerGraphicArr = [];
    const rippleGraphicArr = [];
    this.size = this.size < 99 ? 0 : this.size + 2;
    this.alpha = (101 this.size) / 100;
    this.piontArr.forEach(point => {
        centerGraphicArr.push(new Graphic({ geometry: point, symbol: this.markerSymbol }));
        rippleGraphicArr.push(new Graphic({
            geometry: point,
            symbol: new SimpleMarkerSymbol({
                style: 'circle',
                size: this.size,
                color: [255, 0, 0, this.alpha],
                outline: { color: [0, 0, 0, 0] }
            })
        }));
    });
    this.flashLayer.removeAll();
    this.flashLayer.addMany(centerGraphicArr);
    this.flashLayer.addMany(rippleGraphicArr);
    this.animationId = requestAnimationFrame(this.startAnimation.bind(this));
}

三、车辆轨迹动画

1. 实现步骤

车辆轨迹动画通过设定速度和定时器,使车辆沿着预定的轨迹移动,以下是具体的实现步骤:

(1)引入必要的模块

import { loadModules } from 'esri-loader';
import coordtransform from 'coordtransform';

(2)定义车辆轨迹数据

carPaths: [
    [115.21947154300005, 32.735979284000052],
    [115.22809709000001, 32.746389426000064],
    // ...更多坐标点...
    [115.38855217000005, 32.702488370000026],
    [115.38093780200006, 32.755788297000038],
    [115.38046190900002, 32.77625166100006],
    [115.36190211400003, 32.794811457000037],
    [115.30527094200011, 32.808612131000063],
    [115.22436926700004, 32.809564115000057],
    [115.14489527000001, 32.793383781000045],
    [115.10254086800005, 32.760547219000046],
    [115.0863605290001, 32.727710657000046],
    [115.08649924900008, 32.727710657000046],
    // ...更多坐标点...
];

(3)加载地图和视图

arcgis js 动画
loadModules([ "esri/Map", "esri/views/MapView", "esri/Graphic", "esri/geometry/Point", "esri/layers/GraphicsLayer", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/geometry/Polyline", "esri/geometry/support/webMercatorUtils"], function(Map, MapView, Graphic, Point, GraphicsLayer, SimpleMarkerSymbol, SimpleLineSymbol, Polyline, webMercatorUtils) {
    var map = new Map({ basemap: "streets-navigation-vector" });
    var view = new MapView({ container: "viewDiv", map: map, center: carPaths[0], zoom: 14 });
    view.when(function() { drawPath(view); });
});

(4)绘制路径

function drawPath(view) {
    var polylineSymbol = { type: "simple-line", color: [255, 255, 255, 0.8], style: "short-dot", width: 4 };
    var polyline = { type: "polyline", paths: carPaths, coordinates: carPaths, spatialReference: view.spatialReference };
    var polylineGraphic = new Graphic({ geometry: polyline, symbol: polylineSymbol });
    view.graphics.add(polylineGraphic);
}

ArcGIS JavaScript API 提供了强大的动画功能,可以帮助开发者在地图上展示动态数据和效果,本文介绍了动态脉冲效果和车辆轨迹动画的实现方法,通过这些示例代码,开发者可以快速上手并应用到自己的项目中,随着技术的不断进步,ArcGIS JavaScript API 将会提供更多的功能和优化,进一步提升地图应用的交互性和用户体验。

小伙伴们,上文介绍了“arcgis js 动画”的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。

-- 展开阅读全文 --
头像
为什么App需要网络连接才能使用?
« 上一篇 2024-11-27
为何会出现服务器访问超时的情况?
下一篇 » 2024-11-27
取消
微信二维码
支付宝二维码

发表评论

暂无评论,1人围观

目录[+]