2014-03-19 74 views
0

我想將Google地圖的縮放級別設置爲英里, 現在,我正在使用此代碼,但我認爲這是錯誤的,需要改進。Android Google Maps v2以英里爲單位設置縮放級別

map.animateCamera(CameraUpdateFactory.newLatLngZoom(MYLOC, calculateZoomLevel())); 
public byte calculateZoomLevel() {  
    byte zoom = 1; 
    // if distance is in KMs 
    double E = 40075; 
    // if distance is in Miles 
    //double E = 21638; 
    zoom = (byte) Math.round(Math.log(E/nearByRadius)/Math.log(2) + 1); 
    // to avoid exeptions 
    if (zoom > 21) { 
     zoom = 21; 
    } 
    if (zoom < 1) { 
     zoom = 1; 
    } 
    Log.v(TAG, "zoom level = " + zoom); 
    return zoom; 
} 
+0

你得到了這個解決辦法嗎?因爲我也有相同的要求。 –

回答

2

我有類似的任務,我需要顯示地圖中心和N公里半徑。 這裏是我的解決方案

private CameraUpdate getZoomForDistance(LatLng originalPosition,double distance){ 
    LatLng rightBottom = SphericalUtil.computeOffset(originalPosition,distance,135); 
    LatLng leftTop = SphericalUtil.computeOffset(originalPosition,distance,-45); 
    LatLngBounds sBounds = new LatLngBounds(new LatLng(rightBottom.latitude,leftTop.longitude),new LatLng(leftTop.latitude,rightBottom.longitude)); 
    return CameraUpdateFactory.newLatLngBounds(sBounds,0); 

} 

它使用這個庫android-maps-utils 希望這將幫助你

+0

什麼是computeOffset方法中的第三個參數,在computeOffset定義中它被命名爲標題,但它的用途是什麼? – prateek

相關問題