vue2+arcgis添加流动风场效果
时间: 2023-11-26 10:02:54 浏览: 296
基于Cesium实现的风场(局部)效果,VUE3+TS+Cesium,包括完整demo和源代码,代码未加密/未压缩
5星 · 资源好评率100%
要在 Vue2 中添加流动风场效果,需要使用 ArcGIS API for JavaScript。以下是一些步骤:
1. 安装 ArcGIS API for JavaScript:
```
npm install --save @arcgis/core
```
2. 在 Vue 项目中引入 ArcGIS API for JavaScript:
```javascript
import * as esriLoader from 'esri-loader';
esriLoader.loadModules([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/symbols/SimpleLineSymbol",
"esri/renderers/SimpleRenderer",
"esri/layers/GraphicsLayer",
"esri/Graphic",
"esri/geometry/Polyline",
"esri/symbols/ArrowSymbol3D",
"esri/symbols/LabelSymbol3D",
"esri/symbols/TextSymbol3DLayer",
"esri/symbols/PolygonSymbol3D",
"esri/layers/SceneLayer",
"esri/symbols/SimpleFillSymbol",
"esri/renderers/UniqueValueRenderer",
"esri/symbols/TextSymbol",
"esri/symbols/PictureMarkerSymbol",
"esri/layers/MapImageLayer",
"esri/layers/WebTileLayer",
"esri/widgets/Legend",
"esri/geometry/Point",
"esri/symbols/SimpleMarkerSymbol",
"esri/widgets/Search",
"esri/widgets/Locate",
"esri/tasks/Locator",
"esri/widgets/Measurement",
"esri/widgets/AreaMeasurement2D",
"esri/widgets/DistanceMeasurement2D",
"esri/layers/TileLayer",
"esri/layers/ImageryLayer",
"esri/layers/VectorTileLayer",
"esri/widgets/Home",
"esri/widgets/BasemapGallery",
"esri/widgets/CoordinateConversion",
"esri/widgets/Bookmarks",
"esri/widgets/Print",
"esri/tasks/support/Query",
"esri/tasks/QueryTask",
"esri/tasks/IdentifyTask",
"esri/tasks/support/IdentifyParameters",
"esri/tasks/support/Query",
"esri/tasks/support/StatisticDefinition",
"esri/widgets/Expand",
"esri/core/watchUtils",
"esri/geometry/SpatialReference",
"esri/geometry/Extent",
"esri/geometry/Polygon",
"esri/geometry/geometryEngine",
"esri/config",
"esri/request",
"esri/Color",
"esri/geometry/support/webMercatorUtils",
"esri/geometry/geometryEngineAsync",
"esri/geometry/SpatialReference",
"esri/layers/StreamLayer",
], {
css: true
}).then(([
Map,
MapView,
FeatureLayer,
SimpleLineSymbol,
SimpleRenderer,
GraphicsLayer,
Graphic,
Polyline,
ArrowSymbol3D,
LabelSymbol3D,
TextSymbol3DLayer,
PolygonSymbol3D,
SceneLayer,
SimpleFillSymbol,
UniqueValueRenderer,
TextSymbol,
PictureMarkerSymbol,
MapImageLayer,
WebTileLayer,
Legend,
Point,
SimpleMarkerSymbol,
Search,
Locate,
Locator,
Measurement,
AreaMeasurement2D,
DistanceMeasurement2D,
TileLayer,
ImageryLayer,
VectorTileLayer,
Home,
BasemapGallery,
CoordinateConversion,
Bookmarks,
Print,
Query,
QueryTask,
IdentifyTask,
IdentifyParameters,
StatisticDefinition,
Expand,
watchUtils,
SpatialReference,
Extent,
Polygon,
geometryEngine,
esriConfig,
esriRequest,
Color,
webMercatorUtils,
geometryEngineAsync,
SpatialReference2,
StreamLayer
]) => {
// 将这些模块作为参数传递给组件
// ...
})
```
3. 创建一个 MapView 组件:
```javascript
<template>
<div class="map-container">
<div class="map" ref="map"></div>
</div>
</template>
<script>
export default {
name: 'MapView',
data() {
return {
map: null,
view: null,
streamLayer: null,
windDirection: 0,
windSpeed: 0,
windVariation: 0,
windLayerUrl: ''
}
},
mounted() {
this.initMap();
},
methods: {
async initMap() {
const [Map, MapView] = await esriLoader.loadModules([
"esri/Map",
"esri/views/MapView"]);
// 创建地图
this.map = new Map({
basemap: "streets-navigation-vector"
});
// 创建 MapView
this.view = new MapView({
container: this.$refs.map,
map: this.map,
center: [-118.71511,34.09042],
zoom: 9
});
// 在地图上添加流动风场图层
this.addStreamLayer();
},
async addStreamLayer() {
const [StreamLayer] = await esriLoader.loadModules(["esri/layers/StreamLayer"]);
// 风场图层 URL
this.windLayerUrl = "https://services.arcgis.com/7JUzYtqTbU7kbDUo/arcgis/rest/services/Wind_Stream_Layer/FeatureServer/0";
// 创建从服务器动态下载数据的流动风场图层
this.streamLayer = new StreamLayer({
url: this.windLayerUrl,
purgeOptions: {
displayCount: 10000
},
renderer: {
type: "simple",
symbol: {
type: "simple-line",
color: [255, 255, 255, 0.5],
width: 1,
style: "solid"
}
}
});
// 将图层添加到地图中
this.map.add(this.streamLayer);
// 监听风向和风速的变化
this.streamLayer.on("update", this.updateWind);
},
updateWind(event) {
if (event) {
// 获取风向、风速、风向变化量
this.windDirection = event.windDirection || 0;
this.windSpeed = event.windSpeed || 0;
this.windVariation = event.windVariation || 0;
}
// 根据风向和变化量计算箭头的偏移角度
const angle = this.windDirection + this.windVariation;
// 创建箭头符号
const arrowSymbol = new ArrowSymbol3D({
size: 6,
height: 20,
width: 10,
color: "red"
});
// 创建标签符号
const labelSymbol = new LabelSymbol3D({
symbolLayers: [new TextSymbol3DLayer({
material: {
color: "black"
},
size: 20,
text: `${this.windSpeed} mph`
})]
});
// 创建风向线符号
const lineSymbol = new SimpleLineSymbol({
color: "white",
width: 2,
style: "solid"
});
// 创建面符号
const fillSymbol = new PolygonSymbol3D({
symbolLayers: [
{
type: "extrude",
size: 10,
material: {
color: "red"
}
}
]
});
// 创建风向线
const polyline = new Polyline({
paths: [[[0, 0], [0, 30]]],
spatialReference: {
wkid: 4326
}
});
// 旋转风向线并添加到箭头符号中
arrowSymbol.symbolLayers.add(new Symbol3DLayer({
symbol: lineSymbol.clone().rotate(angle),
width: 10
}));
// 添加箭头符号和标签符号到风场图层
event.features.forEach(feature => {
feature.symbol = arrowSymbol;
feature.labelSymbol = labelSymbol;
feature.geometry = geometryEngineAsync.rotate(feature.geometry, angle, new Point({ x: feature.geometry.extent.center.x, y: feature.geometry.extent.center.y }));
});
// 更新风场图层
this.streamLayer.refresh();
}
}
}
</script>
```
在这个例子中,我们使用了 `StreamLayer` 来实现流动风场效果。在 `addStreamLayer` 方法中,我们创建了一个 `StreamLayer` 对象,并将其添加到地图中。然后,我们监听 `StreamLayer` 的 `update` 事件来更新风向和风速,并计算箭头的偏移角度。最后,我们创建箭头、标签和风向线符号,并将它们添加到 `StreamLayer` 上的每个要素中。注意,这里我们使用了 `ArrowSymbol3D` 和 `LabelSymbol3D` 来创建箭头和标签符号。
在 `updateWind` 方法中,我们首先获取风向、风速和风向变化量,然后计算箭头的偏移角度。接着,我们创建箭头、标签、风向线和面符号,并将它们添加到要素中。最后,我们旋转风向线并更新风场图层。
这就是如何在 Vue2 中使用 ArcGIS API for JavaScript 添加流动风场效果的基本步骤。
阅读全文