自定义几何图元、光锥、三维热力图、路径漫游与路线规划。下面是该示例的核心 script 片段,供参考学习;完整可运行代码请进入在线 Demo 或查看仓库源码。
import * as Cesium from 'cesium'
import { initCesium } from '@/utils/cesiumPluginsExtends/index'
let startMarkerObj, destinationMarkerObj;
let routes = [];
let colors = [
Cesium.Color.fromCssColorString('#01C37C'),
Cesium.Color.SPRINGGREEN,
Cesium.Color.YELLOW,
Cesium.Color.DODGERBLUE,
];
export default {
data () {
return {
startPos: '',
destinationPos: '',
pathPlanList: []
}
},
mounted () {
this.initMap()
},
methods: {
initMap () {
const { viewer,
material,
plugin,
graphics,
draw } = new initCesium(
{
cesiumGlobal: Cesium,
containerId: 'cesiumContainer',
viewerConfig: {
infoBox: false,
shouldAnimate: true,
contextOptions: {
requestWebgl2: true,
}
},
extraConfig: {},
imageryProvider: {
type: "WebMapTileServiceImageryProvider",
option: {
url: "https://t0.tianditu.gov.cn/img_w/wmts?tk=65a5f62a964c1d5b23fa81bc34147973",
layer: "img",
style: "default",
tileMatrixSetID: "w",
format: "tiles",
maximumLevel: 18,
},
},
MapImageryList: [{
type: "WebMapTileServiceImageryProvider",
option: {
url: "https://t0.tianditu.gov.cn/cia_w/wmts?tk=65a5f62a964c1d5b23fa81bc34147973",
layer: "cia",
style: "default",
tileMatrixSetID: "w",
format: "tiles",
maximumLevel: 18,
},
}],
})
this.c_viewer = viewer;
this.draw = draw;
this.material = material;
this.graphics = graphics;
this.plugin = plugin;
this.flyto();
},
/**
* 绘制起点
*/
drawStartPoint () {
let $this = this;
if (startMarkerObj) {
this.draw._drawLayer.entities.remove(startMarkerObj);
startMarkerObj = null;
$this.startPos = '';
}
this.draw.drawPointGraphics({
style: {
image: 'static/data/images/file/start.png'
},
callback: function (pos, entityObj) {
startMarkerObj = entityObj;
$this.startPos = Number(pos[0]['lng']).toFixed(6) + "," + Number(pos[0]['lat']).toFixed(6);
}
})
},
/**
* 绘制终点
*/
drawDestinationPoint () {
let $this = this;
if (destinationMarkerObj) {
this.draw._drawLayer.entities.remove(destinationMarkerObj);
destinationMarkerObj = null;
$this.destinationPos = '';
}
this.draw.drawPointGraphics({
style: {
image: 'static/data/images/file/end.png'
},
callback: function (pos, entityObj) {
destinationMarkerObj = entityObj;
$this.destinationPos = Number(pos[0]['lng']).toFixed(6) + "," + Number(pos[0]['lat']).toFixed(6);
}
})
},
// 步行
computeWalkPath () {
this.computePathPlan({
type: 'walk',
key: '8dc49b1fa65a79d306ef12dae4229842',
url: "https://restapi.amap.com/v3/direction/walking"
})
},
// 骑行
computeCyclePath () {
this.computePathPlan({
type: 'cycle',
key: '8dc49b1fa65a79d306ef12dae4229842',
url: 'https://restapi.amap.com/v4/direction/bicycling'
})
},
/**
* 计算驾车路线
*/
computeDriverPath () {
this.computePathPlan({
type: 'drive',
key: '8dc49b1fa65a79d306ef12dae4229842',
url: "https://restapi.amap.com/v5/direction/driving"
})
},
/**
* 计算步行、骑行路线
*/
computePathPlan ({ type, key, url }) {
this.clearOrgPath();
this.pathPlanList = [];
if (!this.startPos || !this.destinationPos) {
this.$message.error("缺少点位数据!");
return;
}
let startPoint = this.startPos.split(",");
let endPoint = this.destinationPos.split(",");
const queryParams = {
origin: [Number(startPoint[0]), Number(startPoint[1])],
destination: [Number(endPoint[0]), Number(endPoint[1])],
}
if (key) {
queryParams['key'] = key;
}
if (url) {
queryParams['url'] = url;
}
this.plugin.queryPathPlan(type, queryParams, restArr => {
// 拿到结果转换后调用绘制路线
this.createPath(restArr)
}, err => { console.error(err) })
},
/**
* 创建路线
*/
createPath (restArr) {
for (let index = 0; index < restArr.length; index++) {
const item = restArr[index];
const positions = this.graphics.lnglatArrayToCartesians(item.lnglats);
const length = item.distance
const countArrow = length / 350
let route = this.graphics.createLineGraphics({
positions: positions,
oid: index,
name: `方案${index + 1}`,
width: 8,
material: this.material.getCustomMaterialWall({
image: 'static/data/images/Textures/ArrowTransparent.png',
freely: 'cross',
direction: '-', // 顺时针
count: countArrow,
color: colors[index],
duration: 5000,
}),
clampToGround: true,
})
let route1 = this.graphics.createLineGraphics({
positions: positions,
oid: 'base_' + index,
name: `方案${index + 1}`,
width: 12,
material: colors[index],
clampToGround: true,
})
if (route1) {
routes.push(route1);
}
route.tooltip = `方案${index + 1}`
if (route) {
routes.push(route);
}
const inslength = item.instructions.length;
for (let i = 0; i < inslength; i++) {
this.pathPlanList.push({
id: i + 1,
content: item.instructions[i],
});
}
}
},
resetOpera () {
this.draw.removeAll();
this.pathPlanList = [];
this.startPos = "";
this.destinationPos = "";
this.clearOrgPath();
},
clearOrgPath () {
routes.forEach(route => {
if (route) {
this.graphics._graphicsLayer.entities.remove(route)
}
});
routes = [];
},
flyto () {
this.material.flyTo({
position: { x: -1337132.0092982147, y: 5330611.474631115, z: 3228680.029449292 },
orientation: {
heading: Cesium.Math.toRadians(1.0114629015290062),
pitch: Cesium.Math.toRadians(-23.53661660731824),
roll: Cesium.Math.toRadians(0.00324596311071617)
}
})
}
},
beforeUnmount () {
this.c_viewer = null;
this.material = null;
this.graphics = null;
}
}
安装:npm install cesium_dev_kit cesium