1、viewer是操控地图api的开始
<template>
<div id="cesiumContainer">
</div>
</template>
<script setup>
import * as Cesium from 'cesium'
import { onMounted } from 'vue'
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmMDY1ZmUxNi1mMGNhLTQ1NDEtYTU2YS0wNTUwZDJhOWNkNmEiLCJpZCI6MTE1MTg3LCJpYXQiOjE3MDA0NDUxNDB9.wdc1z2bc8UV2q_FpcuB5QKtRx8OYLKq4KI3gDs6-gA8'
onMounted(() => {
// viewer是操控地图api的开始
const viewer = new Cesium.Viewer('cesiumContainer', {
animation: false,//隐藏动画控件
timeline: false,//隐藏时间轴
geocoder: false,//隐藏搜索按钮
homeButton: false,//隐藏主页按钮
sceneModePicker: false,//隐藏投影方式按钮
baseLayerPicker: false,//隐藏图层选择按钮
navigationHelpButton: false,//隐藏帮助按钮
fullscreenButton: false,//隐藏全屏按钮
})
})
</script>
<style scoped>
#cesiumContainer {
width: 100vw;
height: 100vh;
overflow: hidden;
}
</style>
2、坐标转换
<template>
<div id="cesiumContainer">
</div>
</template>
<script setup>
import * as Cesium from 'cesium'
import { onMounted } from 'vue'
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmMDY1ZmUxNi1mMGNhLTQ1NDEtYTU2YS0wNTUwZDJhOWNkNmEiLCJpZCI6MTE1MTg3LCJpYXQiOjE3MDA0NDUxNDB9.wdc1z2bc8UV2q_FpcuB5QKtRx8OYLKq4KI3gDs6-gA8'
onMounted(() => {
// viewer是操控地图api的开始
const viewer = new Cesium.Viewer('cesiumContainer', {})
// 实例化一个笛卡尔坐标(x轴,y轴,z轴) z轴不是高度
const position = new Cesium.Cartesian3(100, 100, 100)
console.log(position)
// 经纬度转笛卡尔(经度,纬度,高度) 高度可以不传 默认是0 高度是相对于地表高度 单位是米
const Cartesian1 = Cesium.Cartesian3.fromDegrees(110, 20)
const Cartesian2 = Cesium.Cartesian3.fromDegrees(110, 20, 100)
console.log(Cartesian1)
console.log(Cartesian2)
// 笛卡尔转经纬度(分两步)
// 1、笛卡尔转弧度坐标
const Cartographic = Cesium.Cartographic.fromCartesian(Cartesian2)
console.log(Cartographic) //高度在弧度坐标中
// 2、弧度坐标转角度坐标
let lon = Cesium.Math.toDegrees(Cartographic.longitude)
let lat = Cesium.Math.toDegrees(Cartographic.latitude)
// let lon = 180 / Math.PI * Cartographic.longitude
// let lat = 180 / Math.PI * Cartographic.latitude
console.log(lon, lat)
// js小数位精度问题 0.1 + 0.2 != 0.3
})
</script>
<style scoped>
#cesiumContainer {
width: 100vw;
height: 100vh;
overflow: hidden;
}
</style>
3、相机 setView、flyTo、lookAt
<template>
<div id="cesiumContainer">
</div>
</template>
<script setup>
import * as Cesium from 'cesium'
import { onMounted } from 'vue'
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmMDY1ZmUxNi1mMGNhLTQ1NDEtYTU2YS0wNTUwZDJhOWNkNmEiLCJpZCI6MTE1MTg3LCJpYXQiOjE3MDA0NDUxNDB9.wdc1z2bc8UV2q_FpcuB5QKtRx8OYLKq4KI3gDs6-gA8'
onMounted(() => {
// viewer是操控地图api的开始
const viewer = new Cesium.Viewer('cesiumContainer', {
})
const position = Cesium.Cartesian3.fromDegrees(110, 20, 20000)
// setView通过定义相机的目的地,直接跳转到目的地
// viewer.camera.setView({
// destination: position, //相机坐标
// orientation: { //默认(0,-90,0)
// heading: Cesium.Math.toRadians(0),
// pitch: Cesium.Math.toRadians(0),
// roll: Cesium.Math.toRadians(0),
// }
// })
// flyTo跟setView相比带飞行动画,可以设置飞行时长
// viewer.camera.flyTo({
// destination: position,
// duration: 3//飞行时长
// })
// lookAt将相机固定在设置的点位上,可以选择视角,不能改变位置
const position2 = Cesium.Cartesian3.fromDegrees(110, 20)
viewer.camera.lookAt(
position2,
new Cesium.HeadingPitchRange(
Cesium.Math.toRadians(0),
Cesium.Math.toRadians(-90),
20000
)
)
})
</script>
<style scoped>
#cesiumContainer {
width: 100vw;
height: 100vh;
overflow: hidden;
}
</style>
5、实体(点、线、多边形、盒子、椭圆、矩形)
<template>
<div id="cesiumContainer">
</div>
</template>
<script setup>
import * as Cesium from 'cesium'
import { onMounted } from 'vue'
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmMDY1ZmUxNi1mMGNhLTQ1NDEtYTU2YS0wNTUwZDJhOWNkNmEiLCJpZCI6MTE1MTg3LCJpYXQiOjE3MDA0NDUxNDB9.wdc1z2bc8UV2q_FpcuB5QKtRx8OYLKq4KI3gDs6-gA8'
onMounted(() => {
// viewer是操控地图api的开始
const viewer = new Cesium.Viewer('cesiumContainer', {
selectionIndicator: false,//隐藏选中框
infoBox: false//隐藏右上角信息框
})
// 二维: 要素---->数据源---->图层---->地图
// 三维: 实体---->地图
// 点
// 写法一:
const point = new Cesium.Entity({
position: Cesium.Cartesian3.fromDegrees(120, 30),
id: 'point', //唯一且必要
point: {
color: Cesium.Color.BLUE, //颜色
pixelSize: 10//像素大小
}
})
console.log(point)
viewer.entities.add(point) //地图添加实体
// 写法二:
const point2 = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(120, 31),
id: 'point2',
point: {
color: Cesium.Color.RED, //颜色
pixelSize: 10//像素大小
}
})
// 标注
const billboard = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116, 40, 30),
billboard: {
image: '/src/assets/position.png',
scale: 0.3,
color: new Cesium.Color(255 / 255, 0 / 255, 0 / 255) // 1对应255
}
})
// 文本
const label = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(112, 30),
label: {
text: 'Cesium',
showBackground: true,
backgroundColor: new Cesium.Color(0.165, 0.165, 0.165, 0.8),
fillColor: Cesium.Color.YELLOWGREEN
}
})
// 线
const polyline = viewer.entities.add({
polyline: {
positions: Cesium.Cartesian3.fromDegreesArray([120, 20, 121, 20, 121, 21]), //得到一个笛卡尔坐标数组
width: 10,
material: Cesium.Color.fromCssColorString('#fff'), //颜色字符串
}
})
// 多边形
const polygon = viewer.entities.add({
polygon: {
hierarchy: {
positions: Cesium.Cartesian3.fromDegreesArray([120, 25, 121, 25, 121, 26]),
},
material: Cesium.Color.fromRandom().withAlpha(0.5), //随机颜色 带透明度
height: 100000, //指定多边形相对于椭球表面的高度
extrudedHeight: 200000, //指定多边形的凸出面相对于椭球面的高度
outline: true,
outlineColor: Cesium.Color.fromCssColorString('#fff'),
fill: false //是否填充
}
})
// 盒子
const box = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(118, 30, 3000),
box: {
dimensions: new Cesium.Cartesian3(100, 200, 300),
material: Cesium.Color.YELLOWGREEN
}
})
// 椭圆
const ellipse = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(119, 30),
ellipse: {
semiMajorAxis: 500,
semiMinorAxis: 300,
rotation: Math.PI / 4,
material: Cesium.Color.BLUEVIOLET
}
})
//矩形
const rectangle = viewer.entities.add({
rectangle: {
coordinates: Cesium.Rectangle.fromDegrees(120, 40, 123, 45), //左下右上
extrudedHeight: 30000,
material: '/src/assets/dog.jpg' //可以用自定义图片填充
}
})
viewer.zoomTo(rectangle) //跳转到实体
})
</script>
<style scoped>
#cesiumContainer {
width: 100vw;
height: 100vh;
overflow: hidden;
}
</style>
###5.1 组合实体
<template>
<div id="cesiumContainer">
</div>
<button @click="toDel" class="btn">删除</button>
</template>
<script setup>
import * as Cesium from 'cesium'
import { onMounted } from 'vue'
// 二维: 100红点---->数据源---->图层---->地图 100蓝点---->数据源---->图层---->地图
// 三维: 把空数组当数据源使用
let redList = []
const toDel = () => {
// 1、直接删除
// viewer.entities.remove(point)
// 2、通过ID删除
// viewer.entities.removeById('point666')
// 3、先查后删
// const entity = viewer.entities.getById('point666')
// viewer.entities.remove(entity)
// 4、全部删除
// viewer.entities.removeAll()
// 5、分类删除
// redList.forEach(item => {
// viewer.entities.remove(item)
// })
// redList = [] //不要忘了
}
let viewer, point
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmMDY1ZmUxNi1mMGNhLTQ1NDEtYTU2YS0wNTUwZDJhOWNkNmEiLCJpZCI6MTE1MTg3LCJpYXQiOjE3MDA0NDUxNDB9.wdc1z2bc8UV2q_FpcuB5QKtRx8OYLKq4KI3gDs6-gA8'
onMounted(() => {
// viewer是操控地图api的开始
viewer = new Cesium.Viewer('cesiumContainer', {
selectionIndicator: false,//隐藏选中框
infoBox: false//隐藏右上角信息框
})
point = viewer.entities.add({
id: 'point666',
position: Cesium.Cartesian3.fromDegrees(121, 30),
point: {
pixelSize: 20,
color: Cesium.Color.RED
}
})
redList.push(point)
let red1 = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(121.0001, 30),
point: {
pixelSize: 20,
color: Cesium.Color.RED
}
})
redList.push(red1)
let red2 = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(121.0002, 30),
point: {
pixelSize: 20,
color: Cesium.Color.RED
}
})
redList.push(red2)
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(121.0004, 30),
point: {
pixelSize: 20,
color: Cesium.Color.BLUE
}
})
viewer.zoomTo(point)
})
</script>
<style scoped>
#cesiumContainer {
width: 100vw;
height: 100vh;
overflow: hidden;
}
.btn {
position: absolute;
left: 50px;
top: 50px;
z-index: 999;
}
</style>
6、数据加载(topojson、kmz)
<template>
<div id="cesiumContainer">
</div>
<button @click="toDel" class="btn">删除</button>
</template>
<script setup>
import * as Cesium from 'cesium'
import { onMounted } from 'vue'
const toDel = () => {
viewer.dataSources.removeAll()
}
let viewer
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmMDY1ZmUxNi1mMGNhLTQ1NDEtYTU2YS0wNTUwZDJhOWNkNmEiLCJpZCI6MTE1MTg3LCJpYXQiOjE3MDA0NDUxNDB9.wdc1z2bc8UV2q_FpcuB5QKtRx8OYLKq4KI3gDs6-gA8'
onMounted(() => {
// viewer是操控地图api的开始
viewer = new Cesium.Viewer('cesiumContainer', {
// selectionIndicator: false,//隐藏选中框
// infoBox: false//隐藏右上角信息框
})
// 加载GeoJson数据
// const linestring = turf.lineString([[-24, 63], [-23, 60], [-25, 65], [-20, 69]]);
// const promise = Cesium.GeoJsonDataSource.load(linestring)
// promise.then(res => {
// const entity = res.entities.values[0]
// viewer.entities.add(entity)
// })
// const polygon = turf.polygon([[[-5, 52], [-4, 56], [-7, 54], [-5, 52]]]);
// Cesium.GeoJsonDataSource.load(polygon).then(res => {
// viewer.dataSources.add(res)
// viewer.zoomTo(res)
// })
// const multiLine = turf.multiLineString([[[0, 0], [4, 4]], [[6, 6], [10, 10]]]);
// const promise = Cesium.GeoJsonDataSource.load(multiLine)
// viewer.dataSources.add(promise)
// viewer.zoomTo(promise)
// 加载topoJson数据
// const promise = Cesium.GeoJsonDataSource.load('/src/assets/usa.topojson')
// viewer.dataSources.add(promise)
// viewer.zoomTo(promise)
// 加载kml数据
const promise = Cesium.KmlDataSource.load('/src/assets/gdp2008.kmz')
viewer.dataSources.add(promise)
viewer.zoomTo(promise)
})
</script>
<style scoped>
#cesiumContainer {
width: 100vw;
height: 100vh;
overflow: hidden;
}
.btn {
position: absolute;
left: 50px;
top: 50px;
z-index: 999;
}
</style>