GeoJSON 点、线、面展示

使用 GeoJSON Source 显示点、线、面,并通过稳定 ID、属性字段和 setData 更新业务数据。

🎯 引言

学完本文,你能把接口数据转换为 GeoJSON,在地图上显示点、路线和区域,并动态替换数据。


🧱 GeoJSON 如何描述业务对象

GeoJSON 用 Geometry 保存空间形状,用 properties 保存名称、状态等业务字段。点表示位置,线表示路径,面表示封闭区域。

const geojson = {
    type: 'FeatureCollection',
    features: [
        {
            type: 'Feature',
            id: 'station-1',
            properties: { name: '站点 A', status: 'online' },
            geometry: {
                type: 'Point',
                coordinates: [116.397, 39.909],
            },
        },
    ],
}

id 用于稳定识别某个 Feature,高亮、选中和增量状态都依赖它。ID 应来自业务主键并保持不变,不要每次请求后随机生成。


📍 分别显示点、线、面

map.addSource('business-data', {
    type: 'geojson',
    data: geojson,
})

map.addLayer({
    id: 'business-points',
    type: 'circle',
    source: 'business-data',
    filter: ['==', ['geometry-type'], 'Point'],
    paint: { 'circle-radius': 7, 'circle-color': '#16a34a' },
})

map.addLayer({
    id: 'business-lines',
    type: 'line',
    source: 'business-data',
    filter: ['==', ['geometry-type'], 'LineString'],
    paint: { 'line-width': 4, 'line-color': '#f97316' },
})

map.addLayer({
    id: 'business-polygons',
    type: 'fill',
    source: 'business-data',
    filter: ['==', ['geometry-type'], 'Polygon'],
    paint: { 'fill-color': '#8b5cf6', 'fill-opacity': 0.25 },
})

同一个混合 Geometry 的 Source 可以通过 geometry-type 拆给不同 Layer。面对象需要轮廓时,再添加一个使用同一 Source 和过滤条件的 line Layer。

下面展示同一份 GeoJSON 中的点、线、面:


🔄 使用 setData 更新数据

const source = map.getSource('business-data') as mapboxgl.GeoJSONSource
source.setData(nextGeoJSON)

setData 替换 Source 数据,已有 Layer 会自动重新绘制,不需要删除重建。频繁更新时应复用 Source,避免重复创建地图资源和事件监听。

下面点击“调用 setData 更新”。位置 A 会移动,地图还会新增位置 C;圆点和文字 Layer 始终是原来的 Layer,没有被删除或重新创建。

当前 Source 有两个点

🔧 把接口数据转换为 GeoJSON

const featureCollection = {
    type: 'FeatureCollection',
    features: rows
        .filter(row => Number.isFinite(row.longitude) && Number.isFinite(row.latitude))
        .map(row => ({
            type: 'Feature',
            id: row.id,
            properties: {
                name: row.name,
                status: row.status,
            },
            geometry: {
                type: 'Point',
                coordinates: [row.longitude, row.latitude],
            },
        })),
}

转换时应校验坐标是否为数字、经纬度顺序是否正确、Geometry 结构是否闭合。Polygon 的每个环首尾坐标必须相同。空 FeatureCollection 可以正常显示为空白业务图层,不必伪造对象。


⚠️ 常见错误表现

  • 坐标写反:对象出现在错误区域或无法显示。
  • LineString 少于两个坐标:Geometry 非法。
  • Polygon 环没有闭合:填充异常或数据被拒绝。
  • Geometry 类型与 Layer 类型不匹配:数据存在但看不到。
  • 字段放在 Geometry 外且不在 properties 中:表达式和点击事件读不到。

🧾 小节总结

  • GeoJSON 用 Geometry 保存形状,用 properties 保存业务字段。
  • Circle、Line、Fill Layer 分别显示点、线、面。
  • 稳定 Feature ID 是选中和高亮的基础。
  • 同一份面数据可以同时绘制填充与轮廓。
  • 更新数据优先调用 setData,不必重建 Source 和 Layer。
  • 接口转换时要校验坐标、Geometry 和空数据。

❓ 知识问答

Q1:properties 可以放嵌套对象吗?

GeoJSON 可以携带 JSON 数据,但 Mapbox 表达式更适合读取字符串、数字和布尔值等扁平字段,复杂对象建议在业务状态中按 ID 查询。

Q2:为什么数据更新后高亮丢失?

新数据若缺少稳定 ID,原来的 Feature 状态无法与新对象对应。

Q3:空数据要删除图层吗?

通常不需要,向 Source 设置空 FeatureCollection 即可保留图层配置。


🧪 小练习

  1. 把三个普通对象转换为 Point Feature,并保留 name 与 status。
  2. 调用 setData 把其中一个点移动到新坐标。
// 请在这里编写代码

🎉 恭喜你已经掌握 GeoJSON 点线面展示啦!