2013-11-01 21 views
12

不久,我想知道如何將攝像頭從當前位置移動到另一個位置。這裏是我的嘗試:安卓 - 谷歌地圖將攝像頭從位置移動到另一個

mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(targetPos, 3)); 
mapView.animateCamera(CameraUpdateFactory.zoomTo(5), 2000, null); 

但谷歌地圖確實將相機從某個位置移動到目標。我如何設置它從A移動到目標,哪個A是我可以設置的某個位置?提前致謝。

+0

這也不適合我,藍點移動到新的位置,但相機從來沒有動畫。 –

回答

15

查看地圖示例中CameraDemoActivity中的代碼。 要去一個位置,你需要有一個CameraPosition。

static final CameraPosition SYDNEY = 
     new CameraPosition.Builder().target(new LatLng(-33.87365, 151.20689)) 
       .zoom(15.5f) 
       .bearing(0) 
       .tilt(25) 
       .build(); 



public void onGoToSydney(View view) { 
    changeCamera(CameraUpdateFactory.newCameraPosition(SYDNEY), new CancelableCallback() { 
     @Override 
     public void onFinish() { 
      Toast.makeText(getBaseContext(), "Animation to Sydney complete", Toast.LENGTH_SHORT) 
        .show(); 
     } 

     @Override 
     public void onCancel() { 
      Toast.makeText(getBaseContext(), "Animation to Sydney canceled", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    }); 
} 


/** 
* Change the camera position by moving or animating the camera depending on the state of the 
* animate toggle button. 
*/ 
private void changeCamera(CameraUpdate update, CancelableCallback callback) { 
    if (mAnimateToggle.isChecked()) { 
     if (mCustomDurationToggle.isChecked()) { 
      int duration = mCustomDurationBar.getProgress(); 
      // The duration must be strictly positive so we make it at least 1. 
      mMap.animateCamera(update, Math.max(duration, 1), callback); 
     } else { 
      mMap.animateCamera(update, callback); 
     } 
    } else { 
     mMap.moveCamera(update); 
    } 
} 
+0

它像一個魅力。非常感謝你。 – R4j

相關問題