// 示例 2:构造瓦片/WMS/缩略图 URL,并抓一张缩略图保存到本地。 // // 用法: // node 02_tiles_and_images.js [mapid] [version] 'use strict'; const fs = require('node:fs'); const path = require('node:path'); const { makeClient, sampleMapid, sampleVersion } = require('./_common'); (async () => { const svc = makeClient(); const mapid = process.argv[2] || sampleMapid(); const version = process.argv[3] || sampleVersion(); console.log(`== 地图 ${mapid}/${version} ==`); const raster = svc.rasterTileUrl({ mapid, version, layer: '0' }); console.log('raster tile URL:\n ', raster); const vector = svc.vectorTileUrl({ mapid, version }); console.log('vector tile URL:\n ', vector); const wms = svc.wmsTileUrl({ mapid, version, width: 512, height: 512 }); console.log('WMS tile URL:\n ', wms); const thumbUrl = svc.thumbnailUrl(mapid, version, { width: 256, height: 256 }); console.log('thumbnail URL:\n ', thumbUrl); const buf = await svc.thumbnailBytes(mapid, version, { width: 256, height: 256 }); const out = path.join(__dirname, `_thumb_${mapid}_${version}.png`); fs.writeFileSync(out, buf); console.log(`缩略图已保存:${path.basename(out)} (${buf.length} bytes)`); })().catch((e) => { console.error('失败:', e.body || e.message || e); process.exit(1); });