mapbox-gl:根据中心点,缩放级别和尺寸计算地图范围 [英] mapbox-gl: calculate map bounds from center point, zoom level and dimensions
问题描述
在Node.js中使用 mapbox-gl-native 我需要找出答案渲染后地图的实际边界.我正在按照项目的README.md中的概述绘制地图:
Using mapbox-gl-native in Node.js I need to find out the actual bounds of a map after rendering. I'm rendering a map as outlined in the project's README.md:
var fs = require('fs');
var path = require('path');
var mbgl = require('mapbox-gl-native');
var sharp = require('sharp');
var options = {
request: function(req, callback) {
fs.readFile(path.join(__dirname, 'test', req.url), function(err, data) {
callback(err, { data: data });
});
},
ratio: 1
};
var map = new mbgl.Map(options);
map.load(require('./test/fixtures/style.json'));
map.render({zoom: 12.25, center: [-122.67, 45.52]}, function(err, buffer) {
if (err) throw err;
// TODO: get map extent
// var extent = map.extent();
// extent is {minX: ..., maxX: ..., minY: ..., maxY: ...} or similar...
map.release();
var image = sharp(buffer, {
raw: {
width: 512,
height: 512,
channels: 4
}
});
// Convert raw image buffer to PNG
image.toFile('image.png', function(err) {
if (err) throw err;
});
});
渲染地图后,我想找出渲染地图的边界是什么.有谁知道使用mapbox-gl-native
是否可以实现此功能,或者是否可以在将来的版本中将其添加到API中?还是有其他方法可以仅从缩放级别,地图的中心和地图的尺寸来计算地图的实际边界?
After the map is rendered, I would like to find out what the bounds of the rendered map are. Does anyone know if this is possible with mapbox-gl-native
or if this is something that could be added to the API in a future version? Or is there another way to calculate the actual bounds of a map simply from the zoom level, the map's center and the map's dimensions?
我使用@JohnS的优秀答案并对其进行了轻微调整,以根据类似的问题来计算给定的缩放级别程度.也许有一种方法可以还原此计算以获得实际范围?
I used and slightly adjusted the excellent answer by @JohnS to a similar question to calculate the zoom level from a given extent. Maybe there is a way to revert this calculation to obtain the actual extent?
推荐答案
我认为您可以尝试 mapbox/geo-viewport
:
I think you can try mapbox/geo-viewport
:
geoViewport.bounds([-122.67, 45.52], 12.25, [512, 512])
这篇关于mapbox-gl:根据中心点,缩放级别和尺寸计算地图范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!