外观
分析功能
643字约2分钟
2025-3-25
本页目标
掌握平台“分析类”功能(量算、缓冲等)的统一用法:它们都以 thing 对象挂在 mars2d.thing.* 下,通过 map.addThing 添加。
目前平台将 量算、地图截图处理 等所有功能都统一封装为 thing 对象。
1. thing 的统一用法
平台的所有管理及分析功能类都继承于 BaseThing 类,均在 mars2d.thing.* 命名空间下。创建后用 map.addThing 添加:
const measure = new mars2d.thing.Measure()
map.addThing(measure)
// 测量长度
measure.distance()1.1 运行效果
新窗口查看
2. 量算
量算有“交互式量算(thing)”和“矢量对象式量算(graphic)”两种,按场景选择:
| 方式 | 类 | 适用场景 |
|---|---|---|
| 交互式量算 | mars2d.thing.Measure | 用户在图上点选、实时量算 |
| 长度测量对象 | mars2d.graphic.DistanceMeasure | 已有坐标、直接渲染测量结果 |
| 面积测量对象 | mars2d.graphic.AreaMeasure | 已有坐标、直接渲染面积结果 |
| 静态量算工具 | mars2d.MeasureUtil | 不渲染,只计算距离/面积/高度差 |
const measure = new mars2d.thing.Measure()
map.addThing(measure)
measure.distance() // 长度测量
// 面积、高度差等测量方式,参数见 Measure API 文档3. 缓冲分析
缓冲分析指“围绕点/线/面生成一定距离的缓冲区”,常用于影响范围、辐射范围等场景。
平台本身不强制某种实现,推荐用开源库 turf.js 做客户端计算,再用 GeoJsonLayer 渲染结果(GeoJsonLayer 支持通过 data 直接传入 GeoJSON 对象):
import * as turf from "@turf/turf"
// 1. 计算缓冲区(以中心点为圆心,6 公里)
const center = turf.point([117.240601, 31.827107])
const buffer = turf.buffer(center, 6, { units: "kilometers" })
// 2. 用 GeoJsonLayer 渲染
const layer = new mars2d.layer.GeoJsonLayer({
data: buffer,
symbol: {
type: "polygon",
styleOptions: {
color: "#00b0ff",
opacity: 0.6,
fillOpacity: 0.2
}
}
})
map.addLayer(layer)提示
turf.js 还提供 nearestPoint(最近点)、isobands 等能力,可组合出更多客户端分析。平台对 turf 等常用可视化/分析库已做接入支持。
4. 其他客户端分析
介绍中提到的“最近点、离散点等值线”等分析,思路同为:用 turf 等库计算 → 用 GeoJsonLayer 或 GraphicLayer 渲染结果。具体示例可参考功能示例与API 文档。
注意事项
注意
缓冲区半径的单位、坐标系要与底图一致;如果底图是 GCJ02/BD09,记得先做坐标纠偏(见坐标系)。
相关
- 相关示例:thing/measure
- 相关 API:Measure、MeasureUtil、GeoJsonLayer
- 延伸阅读:矢量图层、坐标系
