【发布时间】:2019-06-04 16:27:01
【问题描述】:
使用 mapbox-sdk-js 使用以下代码创建的 png 文件不可读,但响应标头显示内容“image/png”且状态代码为 200。这不是我的令牌的问题,否则,如果我使用具有相同参数的 curl 我得到一个正确的 PNG 文件(和一个漂亮的地图)。
你能帮我把回复保存到一个文件吗?谢谢!
const mbxStatic = require('@mapbox/mapbox-sdk/services/static')
const fs = require('fs')
// Get config
const MAPBOX_TOKEN = process.env.MAPBOX_TOKEN
const staticClient = mbxStatic({ accessToken: MAPBOX_TOKEN })
staticClient
.getStaticImage({
ownerId: 'mapbox',
styleId: 'streets-v10',
width: 200,
height: 300,
position: {
coordinates: [-77, 38],
zoom: 5
}
})
.send()
.then(response => {
const image = response.body
console.log(response.headers)
fs.writeFileSync("./map.png", image, 'binary')
}).catch(error => console.log(error));
【问题讨论】: