簡體   English   中英

在縮放級別計算每像素米數的傳單

[英]Leaflet calculating meters per pixel at zoom level

我正在嘗試確定一種方法來計算 Leaflet 中給定縮放級別和地理中心點由 1 個像素表示的米數。 任何人都可以指導我了解所涉及的數學,或者是否有辦法在傳單中開箱即用? 我沒有找到太多。

您可以使用L.MapcontainerPointToLatLng轉換函數來獲取給定像素坐標的 latLngcoordinates。 如果取第一個像素之一和下一個像素,則可以使用L.LatLngdistanceTo實用方法來計算它們之間的距離(以米為單位)。 看下面的代碼(假設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 在編輯之前指出我的錯誤。

您可以使用它來計算每像素的米數:

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

查看openstreetmap.org 頁面的縮放級別 它給出了計算每像素米數的公式:

由一個像素 (S) 表示的距離由下式給出

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

C 是地球的(赤道)周長

z 是縮放級別

y 是您對比例感興趣的緯度。

如果我錯了,請糾正我,恕我直言,每像素的米數 = 以米為單位的地圖高度 / 以像素為單位的地圖高度

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