一、重要说明(你必须先看)
- **你安装的是:tileserver-gl-light **
- 但命令必须使用:
tileserver-gl- 正确:
tileserver-gl --version - 错误:
tileserver-gl-light --version
- 正确:
- 配置文件
config.json必须使用新版格式(旧格式直接报错)
二、安装(仅一次)
bash
npm install -g tileserver-gl-light
验证(一定不要加 -light):
bash
tileserver-gl --version
显示版本号 = 成功。
三、最简启动(无配置文件)
进入你的 mbtiles 文件夹,直接运行:
bash
tileserver-gl 你的地图.mbtiles
访问:
四、🔥 config.json 新版完整教程(唯一正确格式)
1. 完整可直接使用 config.json
json
{
"options": {
"port": 8080,
"host": "0.0.0.0",
"cors": true,
"cacheSize": 200,
"compression": true,
"maxAge": 86400,
"servePreview": true,
"serveStyles": true
},
"data": {
"zwh": {
"mbtiles": "./zwh.mbtiles"
},
"haihe": {
"mbtiles": "./0海河流域地下水一张图矢量切片0514/mydata/1_mbties/haiheliuyudixiashuiyizhangtu.mbtiles"
}
},
"styles": {},
"aliases": {}
}
2. 启动(必须带 --config)
bash
tileserver-gl --config config.json
五、config.json 逐行解释
1️⃣ options(服务配置)
json
"options": {
"port": 8080, // 端口
"host": "0.0.0.0", // 允许局域网访问
"cors": true, // 允许前端跨域(必须开)
"cacheSize": 200, // 缓存 200MB
"compression": true, // 开启压缩
"maxAge": 86400, // 浏览器缓存1天
"servePreview": true, // 开启预览页面
"serveStyles": true // 开启地图样式
}
2️⃣ data(地图数据源 ------ 新版格式!)
每个地图必须写成对象:
json
"data": {
"地图名称": {
"mbtiles": "文件路径"
}
}
你之前报错就是因为这里格式不对!
3️⃣ styles(地图样式,可选)
4️⃣ aliases(路径别名,可选)
六、启动成功后效果
Starting tileserver-gl v5.6.0
Using specified config file from config.json
Starting server
Listening at http://[::]:8080/
Startup complete
七、瓦片访问地址
- zwh 地图:
http://localhost:8080/zwh/{z}/{x}/{y}.pbf - 海河流域:
http://localhost:8080/haihe/{z}/{x}/{y}.pbf
八、最常见错误(你之前遇到的)
❌ 错误 1:命令带 -light
tileserver-gl-light --version // 错误
tileserver-gl --version // 正确
❌ 错误 2:data 直接写路径(旧版格式)
json
"data": {
"zwh": "./zwh.mbtiles" // 错误!
}
✅ 正确新版格式
json
"data": {
"zwh": {
"mbtiles": "./zwh.mbtiles"
}
}
九、你现在 10 秒就能跑起来
-
用我上面给的 config.json
-
运行:
bashtileserver-gl --config config.json
总结(最重要的 3 句话)
- 命令:tileserver-gl(不要加 -light)
- config.json data 必须用 { mbtiles: 路径 }
- 启动:tileserver-gl --config config.json