我有一个缩放级别zoom=Z和一个位置latitude=xlongitude=y,但是我需要使用latitudelongitudelatitudeDeltalongitudeDelta设置区域。

我找到了图片

javascript - 将纬度,经度和zoomLevel转换为latitudeDelta和longitudeDelta-LMLPHP

解释latitudeDeltalongitudeDelta的工作原理,以及公式

zoom = Math.round(Math.log(360 / region.longitudeDelta) / Math.LN2

但是,如何将缩放级别zoom=Z转换为latitudeDeltalongitudeDelta

我猜想我只需要设置latitudeDeltalongitudeDelta,然后根据屏幕尺寸计算其他值?

最佳答案

因此,让zoom的公式取决于longitudeDelta,我们可以表示longitudeDelta带有一些基本的数学规则:

javascript - 将纬度,经度和zoomLevel转换为latitudeDelta和longitudeDelta-LMLPHP

这样,我们将zoom转换为longitudeDelta
要找到latitudeDelta,有不同的方法。我更喜欢找到longitudeDeltalatitudeDelta之间的系数,无论缩放级别如何,该系数始终相同。这是我编写的示例代码。我省略了将缩放级别四舍五入为整数的舍入,以表明计算正确。

// Initial values
var latitudeDelta = 0.004757;
var longitudeDelta = 0.006866;

var coef = latitudeDelta / longitudeDelta; // always the same no matter your zoom

// Find zoom level
var zoomLvlCalculated = calcZoom(longitudeDelta);
console.log(zoomLvlCalculated); // 15.678167523696594

// Find longitudeDelta based on the found zoom
var longitudeDeltaCalculated = calcLongitudeDelta(zoomLvlCalculated);
console.log(calcLongitudeDelta(zoomLvlCalculated));// 0.006865999999999988 which is the same like the initial longitudeDelta, if we omit the floating point calc difference

// Find the latitudeDelta with the coefficient
var latitudeDeltaCalculated = longitudeDeltaCalculated * coef;
console.log(latitudeDeltaCalculated); //0.004756999999999992 which is the same like the initial latitudeDelta, if we omit the floating point calc difference

function calcZoom(longitudeDelta) {
    // Omit rounding intentionally for the example
    return Math.log(360 / longitudeDelta) / Math.LN2;
}

function calcLongitudeDelta(zoom) {
    var power = Math.log2(360) - zoom;
    return Math.pow(2, power);
}

P.S.由于Internet Explorer不支持以2为底的日志,因此您可以使用以下公式以不同的底数(e)进行计算:

javascript - 将纬度,经度和zoomLevel转换为latitudeDelta和longitudeDelta-LMLPHP

10-06 12:14