Frost-Map一个基于 Cesium 的地图工具包,用于在 Web 上创建 3D 地图应用。
功能特性
🗺️ 地图核心 :封装 Cesium Viewer,提供地图初始化和管理
🎨 Vue 组件 :提供 Vue 3 地图组件,支持响应式绑定
📍 视图控制 :支持飞行定位、缩放、视角切换
📐 测量工具 :支持距离测量功能
✏️ 绘制工具 :支持点、线、多边形绘制
💬 弹窗工具 :支持在地图上显示自定义弹窗
安装 快速开始 在 Vue 项目中使用<template>
<div style="width: 100%; height: 600px;">
<FrostMap :options="mapOptions" @ready="onMapReady" />
</div>
</template>
<script setup>
import { FrostMap } from 'frost-map';
const mapOptions = {
animation: false,
baseLayerPicker: true
};
const onMapReady = (mapInstance) => {
console.log('Map is ready:', mapInstance);
};
</script> 在 JavaScript 中使用import { MapClass } from 'frost-map' ;
const map = new MapClass ( {
animation : false
} ) ;
map. init ( document. getElementById ( 'map-container' ) ) ; API 文档 1. MapClass - 地图核心类封装 Cesium Viewer,提供地图初始化、销毁等基础操作。
构造函数
参数
类型
说明
options
Object
Cesium Viewer 配置选项
方法
方法名
参数
返回值
说明
init
container: HTMLElement
MapClass
初始化地图
getViewer
无
Cesium.Viewer | null
获取 Cesium Viewer 实例
destroy
无
void
销毁地图实例
示例const map = new MapClass ( {
baseLayerPicker : false ,
fullscreenButton : true
} ) ;
const viewer = map. init ( document. getElementById ( 'map' ) ) ;
const cesiumViewer = map. getViewer ( ) ;
map. destroy ( ) ;
2. FrostMap - Vue 地图组件基于 MapClass 封装的 Vue 3 组件,提供响应式地图绑定。
Props
属性名
类型
默认值
说明
options
Object
{}
Cesium Viewer 配置选项
Events
事件名
参数
说明
ready
mapInstance: MapClass
地图初始化完成后触发
方法
方法名
返回值
说明
getMap
MapClass | null
获取地图实例
示例<template>
<FrostMap
ref="mapRef"
:options="options"
@ready="onReady"
/>
</template>
<script setup>
import { ref } from 'vue';
import { FrostMap } from 'frost-map';
const mapRef = ref(null);
const options = {
timeline: false
};
const onReady = (map) => {
console.log('Map ready:', map);
};
const getMapInstance = () => {
return mapRef.value?.getMap();
};
</script>
3. ViewController - 视图控制器提供相机控制功能,如飞行定位、缩放、视角切换等。
构造函数new ViewController ( viewer)
参数
类型
说明
viewer
Cesium.Viewer
Cesium Viewer 实例
方法
方法名
参数
返回值
说明
flyTo
position: Object, options?: Object
Promise
飞行到指定位置(带动画)
setView
position: Object, options?: Object
void
设置视图位置(无动画)
zoomIn
distance?: number (默认 1000)
void
放大视图
zoomOut
distance?: number (默认 1000)
void
缩小视图
getCurrentPosition
无
Object | null
获取当前相机位置
lookAt
target: Object, offset?: Cartesian3
void
看向指定目标
参数说明position / target 对象结构:
属性
类型
说明
longitude
number
经度
latitude
number
纬度
height
number (可选)
高度(米),默认 10000
flyTo options:
属性
类型
默认值
说明
duration
number
3
飞行持续时间(秒)
maximumHeight
number
10000
最大飞行高度
示例import { MapClass, ViewController } from 'frost-map' ;
const map = new MapClass ( ) ;
map. init ( document. getElementById ( 'map' ) ) ;
const viewer = map. getViewer ( ) ;
const viewController = new ViewController ( viewer) ;
viewController. flyTo ( {
longitude : 116.4074 ,
latitude : 39.9042 ,
height : 5000
} , {
duration : 2
} ) ;
viewController. zoomIn ( 500 ) ;
const pos = viewController. getCurrentPosition ( ) ;
console. log ( pos) ;
viewController. lookAt ( {
longitude : 116.4074 ,
latitude : 39.9042
} ) ;
4. CesiumMeasure - 测量工具提供距离测量功能,支持鼠标交互绘制测量线。
构造函数new CesiumMeasure ( viewer)
参数
类型
说明
viewer
Cesium.Viewer
Cesium Viewer 实例
方法
方法名
参数
返回值
说明
start
type?: string (默认 'distance')
void
开始测量
stop
无
void
停止测量
clear
无
void
清除测量结果
使用说明
调用 start() 开始测量
左键点击地图添加测量点
鼠标移动预览测量线
右键点击结束测量
示例import { MapClass, CesiumMeasure } from 'frost-map' ;
const map = new MapClass ( ) ;
map. init ( document. getElementById ( 'map' ) ) ;
const viewer = map. getViewer ( ) ;
const measure = new CesiumMeasure ( viewer) ;
measure. start ( ) ;
5. CesiumDrawer - 绘制工具支持在地图上绘制点、线、多边形。
构造函数
参数
类型
说明
viewer
Cesium.Viewer
Cesium Viewer 实例
方法
方法名
参数
返回值
说明
start
mode?: string (默认 'point')
void
开始绘制
finishDrawing
无
void
完成绘制
clear
无
void
清除所有绘制内容
绘制模式
模式
说明
point
绘制点,每次点击添加一个点
polyline
绘制折线,需要至少 2 个点
polygon
绘制多边形,需要至少 3 个点
使用说明
调用 start(mode) 开始绘制
左键点击地图添加点
鼠标移动预览绘制结果
右键点击结束绘制
示例import { MapClass, CesiumDrawer } from 'frost-map' ;
const map = new MapClass ( ) ;
map. init ( document. getElementById ( 'map' ) ) ;
const viewer = map. getViewer ( ) ;
const drawer = new CesiumDrawer ( viewer) ;
drawer. start ( 'point' ) ;
用于在地图上显示自定义弹窗信息。
构造函数
参数
类型
说明
viewer
Cesium.Viewer
Cesium Viewer 实例
方法
方法名
参数
返回值
说明
show
position: Cartesian3, content: string, options?: Object
void
显示弹窗
hide
无
void
隐藏弹窗
参数说明options 对象:
属性
类型
默认值
说明
showCloseButton
boolean
true
是否显示关闭按钮
disablePointerEvents
boolean
true
是否禁用指针事件
disableZoomOnClick
boolean
true
是否禁用点击缩放
示例import { MapClass, Popup } from 'frost-map' ;
const map = new MapClass ( ) ;
map. init ( document. getElementById ( 'map' ) ) ;
const viewer = map. getViewer ( ) ;
const popup = new Popup ( viewer) ;
const position = Cesium. Cartesian3. fromDegrees ( 116.4074 , 39.9042 , 0 ) ;
popup. show ( position, '<h3>Hello World</h3><p>This is a popup</p>' ) ;
7. mapConfig - 地图配置包含默认的 Cesium Viewer 配置选项和相机位置。
配置项{
defaultOptions : {
animation : false ,
baseLayerPicker : true ,
fullscreenButton : true ,
geocoder : true ,
homeButton : true ,
infoBox : true ,
sceneModePicker : true ,
selectionIndicator : true ,
timeline : false ,
navigationHelpButton : false ,
navigationInstructionsInitiallyVisible : false
} ,
defaultCameraPosition : {
longitude : 116.4074 ,
latitude : 39.9042 ,
height : 10000
} ,
imageryProvider : null ,
terrainProvider : null
} 示例import { mapConfig, MapClass } from 'frost-map' ;
const map = new MapClass ( mapConfig. defaultOptions) ;
map. init ( document. getElementById ( 'map' ) ) ;
const viewController = new ViewController ( map. getViewer ( ) ) ;
viewController. flyTo ( mapConfig. defaultCameraPosition) ;
完整示例<template>
<div class="map-container">
<FrostMap ref="mapRef" :options="options" @ready="onMapReady" />
<!-- 控制按钮 -->
<div class="controls">
<button @click="flyToBeijing">飞行到北京</button>
<button @click="startMeasure">开始测量</button>
<button @click="drawPolygon">绘制多边形</button>
<button @click="showPopup">显示弹窗</button>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import {
FrostMap,
ViewController,
CesiumMeasure,
CesiumDrawer,
Popup,
mapConfig
} from 'frost-map';
const mapRef = ref(null);
let viewController = null;
let measure = null;
let drawer = null;
let popup = null;
const options = {
...mapConfig.defaultOptions,
timeline: false
};
const onMapReady = (map) => {
const viewer = map.getViewer();
// 初始化控制器
viewController = new ViewController(viewer);
measure = new CesiumMeasure(viewer);
drawer = new CesiumDrawer(viewer);
popup = new Popup(viewer);
};
const flyToBeijing = () => {
viewController?.flyTo(mapConfig.defaultCameraPosition);
};
const startMeasure = () => {
measure?.start();
};
const drawPolygon = () => {
drawer?.start('polygon');
};
const showPopup = () => {
const position = Cesium.Cartesian3.fromDegrees(
mapConfig.defaultCameraPosition.longitude,
mapConfig.defaultCameraPosition.latitude,
0
);
popup?.show(position, '<h3>北京</h3><p>中国首都</p>');
};
</script>
<style scoped>
.map-container {
width: 100%;
height: 100vh;
position: relative;
}
.controls {
position: absolute;
top: 20px;
left: 20px;
z-index: 100;
display: flex;
gap: 10px;
}
.controls button {
padding: 10px 20px;
background: white;
border: 1px solid #ccc;
border-radius: 4px;
cursor: pointer;
}
.controls button:hover {
background: #f5f5f5;
}
</style>
天地图集成frost-map 提供了便捷的天地图底图配置功能,支持影像图、矢量图和地形图。
使用方式 在 Vue 组件中使用<template>
<FrostMap
:options="mapOptions"
:tianditu-config="tiandituConfig"
@ready="onMapReady"
/>
</template>
<script setup>
import { ref } from 'vue';
import { FrostMap } from 'frost-map';
const mapOptions = {
baseLayerPicker: false // 禁用默认底图选择器
};
const tiandituConfig = ref({
token: 'your-tianditu-token', // 替换为你的天地图 token
type: 'img', // img: 影像, vec: 矢量, ter: 地形
enableLabel: true // 显示标注层
});
const onMapReady = (map) => {
console.log('Map ready:', map);
};
</script> 在 JavaScript 中使用import { MapClass } from 'frost-map' ;
const map = new MapClass ( {
baseLayerPicker : false
} ) ;
map. init ( '#map-container' ) . then ( ( ) => {
map. setTiandituImageryProvider ( {
token : 'your-tianditu-token' ,
type : 'img' ,
enableLabel : true
} ) ;
} ) ; 天地图配置选项
选项
类型
默认值
说明
token
string
-
天地图 token(必填)
type
string
'img'
底图类型:img(影像), vec(矢量), ter(地形)
enableLabel
boolean
true
是否显示标注层
获取天地图 Token
访问 天地图官网
注册账号并登录
进入控制台 -> 我的应用 -> 创建应用
获取 Token
依赖说明由于 cesium 和 vue 被配置为外部依赖(externals),使用时需要确保在项目中正确引入:
通过 CDN 引入< script src = " https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js" > </ script>
< script src = " https://cesium.com/downloads/cesiumjs/releases/1.100/Build/Cesium/Cesium.js" > </ script>
< link href = " https://cesium.com/downloads/cesiumjs/releases/1.100/Build/Cesium/Widgets/widgets.css" rel = " stylesheet" > 通过 npm 安装
许可证MIT License