如果在代码中避免太多


function get_map_zoom_level(distance) {
    var zoom = 1;
    if (distance == 0)
        zoom = 11;
    else if (distance == 1)
        zoom = 15;
    else if (distance > 880 && distance <= 1760)
        zoom = 14;
    else if (distance > 1760 && distance <= 3520)
        zoom = 13;
    else if (distance > 3520 && distance <= 7040)
        zoom = 12;
    else if (distance > 7040 && distance <= 14080)
        zoom = 11;
    else if (distance > 14080 && distance <= 28160)
        zoom = 10;
    else if (distance > 28160 && distance <= 56320)
        zoom = 9;
    else if (distance > 56320 && distance <= 112640)
        zoom = 8;
    else if (distance > 112640 && distance <= 225280)
        zoom = 7;
    else if (distance > 225280 && distance <= 450560)
        zoom = 6;
    else if (distance > 450560 && distance <= 889120)
        zoom = 5;
    else if (distance > 889120 && distance <= 1718240)
        zoom = 4;
    else if (distance > 1718240)
        zoom = 3;
    return zoom;
}

我想知道是否有任何方法可以避免此代码中的许多(如果有的话)。基本上我想根据距离找出缩放级别,这段代码运行良好,但我需要一些更好的方法来做到这一点。

这应该有效:

function get_map_zoom_level(distance) {
var zoom = 15;
var temp=880;
if (distance == 0)
    zoom = 11
while (distance > temp){
    temp=temp*2;
    zoom--;
}
return zoom;
}

当您有大量if语句比较与您的相同数据时,通常的方法是创建一个 Array,其中每个条目都包含要比较的数据和结果,换句话说,最小距离、最大距离和缩放级别。

若要进行比较,请使用 for 循环遍历数组,检查每个数组的值。 如果找到匹配的条目,则设置结果并break出循环。

这并不比你这样做的方式更有效或更快,但这意味着你可以有任意数量的选项,你的实际代码不会变得更大或更复杂。

以下是您可以使用您的方法执行此操作的方法:

function get_map_zoom_level(distance) {
    // create the array of values. Each entry can be either an array or an object, doesn't matter too much.
    // I'll use an array set up as [minDistance, maxDistance, zoom]
    var zoomLevels = [
        [880, 1760, 14],
        [1760, 3520, 13],
        [3520, 7040, 12],
        [7040, 14080, 11],
        // ... etc.
    ]
    var zoom = 1;
    // special cases
    if (distance == 0)
        zoom = 11;
    else if (distance == 1)
        zoom = 15;
    else if (distance > 1718240)
        zoom = 3;
    else {
        for (var i = 0; i < zoomLevels.length; i++) {
            var zoomLevel = zoomLevels[i];
            if (distance > zoomLevel[0] && distance <= zoomLevel[1]) {
                zoom = zoomLevel[2];
                // found a match - stop the loop
                break;
            }
        }
    }
    return zoom;
}

顺便说一句,缩放级别 11 似乎有错误,7040 和 1480 不匹配。

你可以有一个这样的对象:

var zooms = [ { min:1480 , max:28160} ,  // array index is zoom level
              // ...
              { min:880 , max:1760} ];

然后迭代它,直到找到缩放

for(var z = 1 ; z <= MAX_ZOOM ; z++) {
    if (distance > zooms[z].min && distance <= zooms[z].max) {
        return z;
    }
}

在Java中,它会更容易。

以下代码保持完全相同的边缘情况(显式,例如距离为 0 ->缩放为 11,隐式,例如距离为 42 ->缩放为 1),您的代码也具有相同的顺序,但是,如果我们不处理边缘情况,则使用升序的距离数组决定适用的中间范围, 表示(范围)的包容性上限。通过从头开始迭代数组来处理独占下限,因为值是升序的。

function get_map_zoom_level(distance) {
    var distanceBreakpoints = [1760, 3520, 7040, 14080, 28160, 56320, 112640, 225280, 450560, 889120, 1718240];
    var zoom = 1;
    if (distance == 0)
        zoom = 11;
    else if (distance == 1)
        zoom = 15;
    else if (distance > 880 && distance <= 1718240) {
        for (var i = 0; i < distanceBreakpoints.length; ++i) {
            if (distance <= distanceBreakpoints[i]) {
                zoom = 14 - i;
                break;
            }
        }
    } else if (distance > 1718240)
        zoom = 3;
    return zoom;
}

相关内容

最新更新