2014-02-26 134 views
0

請問我有問題, 如何設置依賴於當前速度的縮放級別? 例如,當移動緩慢。放大到近處,快速時放大到遠處。Android MapView動態設置縮放級別

這裏是我的算法:

private int calculateZoomLevelBySpeed(float speed) { 
    int zoomLevelMax = mapView.getMaxZoomLevel(); 
    // default 
    int zoomLevel = (zoomLevelMax > 0) ? (int)zoomLevelMax/2 : 4; 

    if (speed > 0) { 
     // 300km theoretically max/max zoom steps 
     double step = 300/zoomLevelMax; 
     zoomLevel = (int) Math.round(speed/step); 
     // reverse 
     zoomLevel = zoomLevelMax - zoomLevel; 
     zoomLevel = Math.min(zoomLevelMax, zoomLevelMax); 
     zoomLevel = Math.max(zoomLevelMax, 1); 
    } 
    return zoomLevel; 
} 

這不起作用,我該怎麼辦呢?

在此先感謝。

+0

嗨拉茲洛,你已經發布的代碼是計算變焦級別或設置縮放級別? – Vijju

+0

'zoomLevel = Math.min(zoomLevelMax,zoomLevelMax);'? – cosmincalistru

+0

也許這是問題,我會嘗試使用固定代碼, zoomLevel = Math.min(zoomLevel,zoomLevelMax); – Laszlo

回答

-1

試試這個,我真的沒有測試它,但我希望它能幫助

private int calculateZoomLevelBySpeed(float speed) 
{ 
    //assuming this line returns the max zoom level on maps 
    int zoomLevelMax = mapView.getMaxZoomLevel(); 
    // default 
    int zoomLevel = (zoomLevelMax > 0) ? (int)zoomLevelMax/2 : 4; 

    if (speed > 0) { 
     // 300km theoretically max/max zoom steps 
     double step = (speed/300) * zoomLevelMax; 
    } 
    return Math.Round(zoomLevel); 
} 
+0

正如我所看到的,zoomLevel不受此代碼中的步驟影響,不會對可以修改zoomLevel的步驟進行任何操作 – Laszlo

-1

試試這樣說:

private int calculateZoomLevelBySpeed(float speed) { 
    int zoomLevelMax = mapView.getMaxZoomLevel(); 

    if (speed > 0) { 
     // 300km theoretically max/max zoom steps 
     double step = 300/zoomLevelMax; 
     zoomLevel = (int) Math.round(speed/step); 
     // reverse 
     zoomLevel = zoomLevelMax - zoomLevel; 
    } 
    return zoomLevel; 
}