我有一个缩放级别zoom=Z
和一个位置latitude=x
,longitude=y
,但是我需要使用latitude
,longitude
,latitudeDelta
和longitudeDelta
设置区域。
我找到了图片
解释latitudeDelta
和longitudeDelta
的工作原理,以及公式
zoom = Math.round(Math.log(360 / region.longitudeDelta) / Math.LN2
但是,如何将缩放级别
zoom=Z
转换为latitudeDelta
和longitudeDelta
?我猜想我只需要设置
latitudeDelta
或longitudeDelta
,然后根据屏幕尺寸计算其他值? 最佳答案
因此,让zoom
的公式取决于longitudeDelta
,我们可以表示longitudeDelta
带有一些基本的数学规则:
这样,我们将zoom
转换为longitudeDelta
。
要找到latitudeDelta
,有不同的方法。我更喜欢找到longitudeDelta
和latitudeDelta
之间的系数,无论缩放级别如何,该系数始终相同。这是我编写的示例代码。我省略了将缩放级别四舍五入为整数的舍入,以表明计算正确。
// 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)进行计算: