我正在制作一个应用程序,用于在地图上绘制运行路线并将它们保存到 mongodb 数据库中。
目前,我正在使用 puppeteer 访问我的应用程序中的路线,并将坐标作为查询字符串传递给地图组件。加载地图后,我截取屏幕截图,将返回的 Buffer 转换为 base64 编码的字符串,将其保存到数据库中,并使用该字符串在前端显示图像。
整个过程的中间件是这样的:
exports.screenshotMap = async (req, res, next) => {
try {
const { lineFeatures } = req.body;
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
// open new browser
const page = await browser.newPage();
// create array of coordinates from geojson features
const coords = lineFeatures.map(line => line.geometry.coordinates);
// reduce to 2D array of [lat, lon] coords
const flattenedCoords = coords.reduce((accum, arr) => {
return accum.concat(arr);
}, []);
// …Run Code Online (Sandbox Code Playgroud)