返回顶部
m

mapbox-maplibre-migrationMapLibre迁移指南

Guide for migrating from MapLibre GL JS to Mapbox GL JS, covering API compatibility, token setup, style configuration, and the benefits of Mapbox's official support and ecosystem

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
95
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

mapbox-maplibre-migration

MapLibre 到 Mapbox 迁移技能

从 MapLibre GL JS 迁移到 Mapbox GL JS 的专业指南。涵盖共同历史、API 兼容性、迁移步骤以及 Mapbox 平台的优势。

理解分支

历史

MapLibre GL JSMapbox GL JS v1.13.0 的开源分支,于 2020 年 12 月 Mapbox 从 v2.0 开始更改许可证时创建。

时间线:

  • - 2020 年前: Mapbox GL JS 是开源的(BSD 许可证)
  • 2020 年 12 月: Mapbox GL JS v2.0 引入专有许可证
  • 2020 年 12 月: 社区将 v1.13 分支为 MapLibre GL JS
  • 现在: 两个库都在积极开发中

关键洞察: API 约 95% 相同,因为 MapLibre 最初是 Mapbox 的分支。大多数代码只需极少的更改即可在两个库中运行,使迁移变得简单直接。

为什么迁移到 Mapbox?

选择 Mapbox GL JS 的令人信服的理由:

  • - 官方支持与 SLA:企业级支持,保证响应时间
  • 卓越的瓦片质量:全球覆盖且频繁更新的顶级矢量瓦片
  • 更好的卫星图像:高分辨率、最新的卫星和航空影像
  • 丰富的生态系统:与 Mapbox Studio、API 和服务的无缝集成
  • 高级功能:交通感知路由、逐向导航、高级数据集
  • 地理编码与搜索:世界级的地址搜索和地点查询
  • 导航 SDK:具有实时交通信息的移动导航
  • 无需瓦片基础设施:无需托管或维护自己的瓦片服务器
  • 定期更新:持续改进和新功能
  • 专业服务:为复杂项目提供 Mapbox 解决方案团队支持

Mapbox 提供慷慨的免费层级: 每月 50,000 次地图加载,使许多应用无需成本即可使用。

快速对比

方面Mapbox GL JSMapLibre GL JS
许可证专有 (v2+)BSD 3-Clause (开源)
支持
官方商业支持 | 社区支持 | | 瓦片 | 高级 Mapbox 矢量瓦片 | OSM 或自定义瓦片源 | | 卫星图像 | 高质量全球影像 | 需要自定义源 | | 令牌 | 必需(访问令牌) | 可选(取决于瓦片源) | | API | 完整的 Mapbox 生态系统 | 需要第三方服务 | | Studio | 完全集成 | 无原生集成 | | 3D 地形 | 内置,含高级数据 | 可用(需要数据源) | | 地球视图 | v2.9+ | v3.0+ | | API 兼容性 | 与 MapLibre 约 95% 兼容 | 与 Mapbox 约 95% 兼容 | | 包大小 | 约 500KB | 约 450KB | | 设置复杂度 | 简单(只需添加令牌) | 需要设置瓦片源 |

逐步迁移

1. 创建 Mapbox 账户

  1. 1. 在 mapbox.com 注册
  2. 从账户仪表板获取访问令牌
  3. 查看定价:免费层级包括每月 50,000 次地图加载
  4. 记录您的令牌(公共令牌以 pk. 开头)

2. 更新包

bash

移除 MapLibre


npm uninstall maplibre-gl

安装 Mapbox

npm install mapbox-gl

3. 更新导入

javascript
// 之前 (MapLibre)
import maplibregl from maplibre-gl;
import maplibre-gl/dist/maplibre-gl.css;

// 之后 (Mapbox)
import mapboxgl from mapbox-gl;
import mapbox-gl/dist/mapbox-gl.css;

或使用 CDN:

html




4. 添加访问令牌

javascript
// 在地图初始化之前添加
mapboxgl.accessToken = pk.yourmapboxaccess_token;

令牌最佳实践:

  • - 使用环境变量:process.env.VITEMAPBOXTOKEN 或 process.env.NEXTPUBLICMAPBOX_TOKEN
  • 在 Mapbox 仪表板中添加 URL 限制以确保安全
  • 客户端代码使用公共令牌(pk.*)
  • 切勿将令牌提交到 git(添加到 .env 和 .gitignore)
  • 如果令牌泄露,请轮换

有关全面的令牌安全指南,请参阅 mapbox-token-security 技能。

5. 更新地图初始化

javascript
// 之前 (MapLibre)
const map = new maplibregl.Map({
container: map,
style: https://demotiles.maplibre.org/style.json, // 或您的自定义样式
center: [-122.4194, 37.7749],
zoom: 12
});

// 之后 (Mapbox)
mapboxgl.accessToken = pk.yourmapboxaccess_token;

const map = new mapboxgl.Map({
container: map,
style: mapbox://styles/mapbox/standard, // Mapbox 样式
center: [-122.4194, 37.7749],
zoom: 12
});

6. 更新样式 URL

Mapbox 提供专业设计、维护的样式:

javascript
// Mapbox 内置样式
style: mapbox://styles/mapbox/standard; // Mapbox Standard (默认)
style: mapbox://styles/mapbox/standard-satellite; // Mapbox Standard Satellite
style: mapbox://styles/mapbox/streets-v12; // Streets v12
style: mapbox://styles/mapbox/satellite-v9; // 卫星图像
style: mapbox://styles/mapbox/satellite-streets-v12; // 混合
style: mapbox://styles/mapbox/outdoors-v12; // 户外/休闲
style: mapbox://styles/mapbox/light-v11; // 浅色主题
style: mapbox://styles/mapbox/dark-v11; // 深色主题
style: mapbox://styles/mapbox/navigation-day-v1; // 导航(白天)
style: mapbox://styles/mapbox/navigation-night-v1; // 导航(夜晚)

自定义样式:
您还可以从 Mapbox Studio 创建和使用自定义样式:

javascript
style: mapbox://styles/your-username/your-style-id;

7. 更新所有引用

将所有 maplibregl 引用替换为 mapboxgl:

javascript
// 标记
const marker = new mapboxgl.Marker() // 之前是:maplibregl.Marker()
.setLngLat([-122.4194, 37.7749])
.setPopup(new mapboxgl.Popup().setText(San Francisco))
.addTo(map);

// 控件
map.addControl(new mapboxgl.NavigationControl(), top-right);
map.addControl(new mapboxgl.GeolocateControl());
map.addControl(new mapboxgl.FullscreenControl());
map.addControl(new mapboxgl.ScaleControl());

8. 更新插件(如使用)

某些 MapLibre 插件应替换为 Mapbox 版本:

MapLibre 插件Mapbox 替代方案
@maplibre/maplibre-gl-geocoder@mapbox/mapbox-gl-geocoder
@maplibre/maplibre-gl-draw
@mapbox/mapbox-gl-draw |
| maplibre-gl-compare | mapbox-gl-compare |

示例:

javascript
// 之前 (MapLibre)

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 mapbox-maplibre-migration-1775921291 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 mapbox-maplibre-migration-1775921291 技能

通过命令行安装

skillhub install mapbox-maplibre-migration-1775921291

下载

⬇ 下载 mapbox-maplibre-migration v1.0.0(免费)

文件大小: 13.84 KB | 发布时间: 2026-4-12 10:30

v1.0.0 最新 2026-4-12 10:30
Initial release: provides a comprehensive step-by-step migration guide from MapLibre GL JS to Mapbox GL JS.

- Explains the shared history and API compatibility between MapLibre and Mapbox.
- Details the reasons and benefits for migrating to Mapbox, including features and ecosystem.
- Offers clear migration instructions: updating packages, imports, token setup, and style configuration.
- Provides tables for feature and API comparisons between libraries.
- Lists best practices for access token security and plugin migration.
- Delivers concise code examples for all key steps.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部