简体   繁体   English

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

[英]Leaflet calculating meters per pixel at zoom level

I am trying to determine a way to calculate the number of meters represented by 1 pixel at a given zoom level and geo centerpoint in Leaflet.我正在尝试确定一种方法来计算 Leaflet 中给定缩放级别和地理中心点由 1 个像素表示的米数。 Could anyone direct me to the math involved or if there is a way to do this out of the box in leaflet?任何人都可以指导我了解所涉及的数学,或者是否有办法在传单中开箱即用? I am not finding much out there.我没有找到太多。

You can use the containerPointToLatLng conversion function of L.Map to get the latLngcoordinates for a given pixelcoordinate.您可以使用L.MapcontainerPointToLatLng转换函数来获取给定像素坐标的 latLngcoordinates。 If you take one of the first pixel, and one of the next, you can use the distanceTo utility method of L.LatLng to calculate the distance in meters between them.如果取第一个像素之一和下一个像素,则可以使用L.LatLngdistanceTo实用方法来计算它们之间的距离(以米为单位)。 See the following code (assuming map is an instance of L.Map):看下面的代码(假设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)

That should work, thanks to Jarek Piórkowski for pointing my mistake before the edit.这应该可行,感谢 Jarek Piórkowski 在编辑之前指出我的错误。

您可以使用它来计算每像素的米数:

metresPerPixel = 40075016.686 * Math.abs(Math.cos(map.getCenter().lat * Math.PI/180)) / Math.pow(2, map.getZoom()+8);

Take a look at openstreetmap.org page on zoom levels .查看openstreetmap.org 页面的缩放级别 It gives this formula for calculating the meters per pixel:它给出了计算每像素米数的公式:

The distance represented by one pixel (S) is given by由一个像素 (S) 表示的距离由下式给出

S=C*cos(y)/2^(z+8) where... S=C*cos(y)/2^(z+8)其中...

C is the (equatorial) circumference of the Earth C 是地球的(赤道)周长

z is the zoom level z 是缩放级别

y is the latitude of where you're interested in the scale. y 是您对比例感兴趣的纬度。

Correct me if I am wrong, IMHO, the number of meters per pixel = map height in meters / map height in pixels如果我错了,请纠正我,恕我直言,每像素的米数 = 以米为单位的地图高度 / 以像素为单位的地图高度

function metresPerPixel() {
    const southEastPoint = map.getBounds().getSouthEast();
    const northEastPoint = map.getBounds().getNorthEast();
    const mapHeightInMetres = southEastPoint.distanceTo(northEastPoint);
    const mapHeightInPixels = map.getSize().y;

    return mapHeightInMetres / mapHeightInPixels;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM