Search code examples
androidgoogle-mapsaccelerometer

Scroll android maps pragmatically


I am trying to build an android app to scroll over android maps API v2 in android studio. I want to scroll the maps in all different directions using the phone's Accelerometer sensor. For example if the phone is tilted 45 degree to the right, I want the map to be contentiously moving in 45 degree to the right. From what I already found I can only moveCamera (scroll) by latlong target or by pixels, Is there any way to do so using the angle of X , Y coordination? These are samples of what I have tried: 1- Move camera by latlang target.

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
            .zoom(20)                   // Sets the zoom
            .bearing(20)                // Sets the orientation of the camera to east
            .tilt(10)                   // Sets the tilt of the camera to 30 degrees
            .build();                   // Creates a CameraPosition from the builder

    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 1000, null);

2- Find phone angle from X,Y using Accelerometer:

        float X = (float)Math.round(event.values[0]);
        float Y= (float)Math.round(event.values[1]);
        float angle = (float) (Math.atan2(X, Y)/(Math.PI/180));

I would appreciate any new idea to do this or a way to moveCamera by angle.


Solution

  • This method will just move the camera by pixels equal to the magnitude of the device tilt, over one second.

    mMap.animateCamera(CameraUpdateFactory.scrollBy(x, y), 1000, null);