javascript - 传单在缩放级别计算每像素米数

标签 javascript leaflet geo

我正在尝试确定一种方法来计算 Leaflet 中给定缩放级别和地理中心点下 1 个像素表示的米数。谁能指导我涉及到的数学问题,或者传单中是否有开箱即用的方法?我在那里找不到太多东西。

最佳答案

您可以使用 containerPointToLatLng L.Map 的转换函数,用于获取给定像素坐标的 latLng 坐标。如果你取第一个像素和下一个像素,你可以使用 distanceTo L.LatLng 的实用方法来计算它们之间的距离(以米为单位)。看下面代码(假设map是L.Map的一个实例):

var centerLatLng = map.getCenter(); // get map center
var pointC = map.latLngToContainerPoint(centerLatLng); // convert to containerpoint (pixels)
var pointX = [pointC.x + 1, pointC.y]; // add one pixel to x
var pointY = [pointC.x, pointC.y + 1]; // add one pixel to y

// convert containerpoints to latlng's
var latLngC = map.containerPointToLatLng(pointC);
var latLngX = map.containerPointToLatLng(pointX);
var latLngY = map.containerPointToLatLng(pointY);

var distanceX = latLngC.distanceTo(latLngX); // calculate distance between c and x (latitude)
var distanceY = latLngC.distanceTo(latLngY); // calculate distance between c and y (longitude)

这应该行得通,感谢 Jarek Piórkowski 在编辑前指出我的错误。

关于javascript - 传单在缩放级别计算每像素米数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27545098/

相关文章:

php - php foreach 循环中的 Javascript getElementById

javascript - 数组索引在 $.get 中不起作用

javascript - 找不到 React-Leaflet 标记文件

javascript - 如何删除传单中的标记分组?

javascript - 按百分比获取两点之间连线上的经纬度点

r - 如何创建地理空间/时间数据的动画

javascript - 具有非顺序索引数组的 .splice() 数组 - javascript

javascript - AngularJS - 如何禁用 OPTION 请求?

javascript - 传单标记簇 : open popup on childmarker after zoom in

python - python中基于geo查询cloudant数据库的建议