获得标记边界Gmap V2后无法更新倾斜

时间:2013-09-17 16:51:30

标签: android google-maps-android-api-2

我正在使用它来获取地图上标记的边界:

markerList.add(m);
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker marker : markerList) {
            builder.include(marker.getPosition());
        }
        LatLngBounds bounds = builder.build();
        CameraUpdate updatecamera = CameraUpdateFactory.newLatLngBounds(bounds, 50);
        map.animateCamera(updatecamera);

获得更新后,我想将地图倾斜30度。

过去我用过这个来倾斜相机,但它没有使用边界功能:

CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(meLoc).zoom(6).bearing(0).tilt(30).build();
                map.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));

如何在获得界限后添加此倾斜?

由于

3 个答案:

答案 0 :(得分:3)

正如您所说,Android Maps API v2目前不支持使用边界和倾斜信息动画相机。

一种解决方法是使用以下步骤:

  1. 使用边界执行要执行的相机动画,但忽略倾斜
  2. 听取上述相机动画的结束,获取当前相机位置,然后执行倾斜
  3. 这具有将摄像机移动到边界同时向下看(即,倾斜= 0),然后将摄像机倾斜到位的效果。我发现这在一些用例中运行良好,实际上看起来不错,但在其他用途​​中它有点尴尬。它可能适用于您的特定用例,也可能不适用。

    为了实现这一点,我们假设您有一个实现地图的主类MapScreen.java。您需要更改它以包括摄像机监听器界面,摄像机位置参考,活动参考,您想要使用的初始倾斜值(您可以在运行时交替设置),以及默认的摄像机填充:

    public class MapScreen extends FragmentActivity implements GoogleMap.OnCameraChangeListener {
        ...
        private GoogleMap map;
        public static CameraPosition lastCameraPosition;
        public static MapScreen mapScreen;
        public static float CAMERA_INITIAL_TILT = 30.0f;
        public static int CAMERA_PADDING = 100;       
        ...
        public void onCreate(Bundle savedInstanceState) {
            ...
            mapScreen = this;
            ...
            //Set up map object here like normal
            map = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
            ...
            //Set camera change listener
            map.setOnCameraChangeListener(this);
        }
        ...
    }
    

    在本课程中,您还需要添加一个方法来监听并在第一个动画停止时保存相机的位置:

    @Override
    public void onCameraChange(CameraPosition position) {
        //Save the last camera position so we can reference it when tilting the camera following animations
        lastCameraPosition = position;      
    }
    

    此外,添加一个可以引用的内部类,在第一个动画结束后执行倾斜(稍微延迟以获得正确的CameraPosition):

    public class tiltOnFinishAnimation implements CancelableCallback {
    
        @Override
        public void onCancel() {
            //Do nothing
        }
    
        @Override
        public void onFinish() {
            //We want to run the tilt animation, but the CameraPosition needed to center the camera in the right place
            //is only available after the onFinish() method completes.  So, we delay this by 10 ms to let the CameraPosition update
            final Handler handler = new Handler(); 
            handler.postDelayed(new Runnable() { 
              public void run() {
                  mapScreen.runOnUiThread(new Runnable() {
                        public void run() {             
                            if(lastCameraPosition != null){
                                //Finish with a tilt
                                CameraPosition cameraPosition = new CameraPosition.Builder()
                                    .target(lastCameraPosition.target)
                                    .zoom(lastCameraPosition.zoom)                   
                                    .bearing(lastCameraPosition.bearing)
                                    .tilt(CAMERA_INITIAL_TILT)
                                    .build();                   
                                //Perform the tilt!
                                mapScreen.map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));                             
                            }
                        }
                    });
              } 
            }, 10);         
        }       
    }
    

    最后,当您想要移动相机时,执行您的普通代码,但首先要确定根据方向正确设置相机动画所需的一些地图视图属性,并包含对第一部分时执行的回调的引用没有倾斜停止的动画:

    //Get the View height and width, so we don't exceed the screen size after padding for the camera updates
    int width;
    int height;
    if(mapFragment != null){
        width = mapFragment.getView().getWidth();
        height = mapFragment.getView().getHeight();
    }else{      
        //If the map fragment hasn't been instantiated yet, use the entire display
        if (android.os.Build.VERSION.SDK_INT >= 13){
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            width = size.x;
            height = size.y;
        }else{
            Display display = getWindowManager().getDefaultDisplay();
            width = display.getWidth();
            height = display.getHeight();
        }
    }
    
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){               
        //Use verticle size for padding 
        CAMERA_PADDING = (int) (height * 0.2f);
    }else{
        //Use horizontal size for padding
        CAMERA_PADDING = (int) (width * 0.2f);
    }    
    
    //Your code
    markerList.add(m);
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (Marker marker : markerList) {
        builder.include(marker.getPosition());
    }
    LatLngBounds bounds = builder.build();
    
    //Here's the new code below that triggers first the movement to the bounds, then the tilt (as a callback)
    map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, CAMERA_PADDING), new tiltOnFinishAnimation());
    

答案 1 :(得分:0)

要设置GoogleMap TiltBound所有标记,您需要使用以下代码。

googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 75));
CameraPosition camPos = new CameraPosition.Builder(googleMap.getCameraPosition())
                              .target(bounds.getCenter())
                              .bearing(bearing).tilt(45).build();
googleMap.animateCamera(CameraUpdateFactory
                              .newCameraPosition(camPos));

答案 2 :(得分:0)

关键点是没有animateCamera(..)后跟另一个animateCamera(..)没有足够的延迟,因为它们重叠。因此,您最好首先使用moveCamera(..),然后使用animateCamera(..),或者对第二个animateCamera(..)使用一些延迟技术,例如new Handler().postDelayed(..)