Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android maps auto-rotate

Tags:

If you open the Google maps app, there is a button on the top right of the screen that you can press to center the map on your current location. The button's icon then changes. If you press the same button again, the map auto-rotates based on your compass heading. In other words, the map becomes egocentric (as opposed to allocentric AKA north always up).

Google recently launched maps API V2 for Android and I certainly like it more than the old one. By default, android maps V2 will include the "center on location" button. However, pressing it more than once does not enable auto-rotation; it merely tries to center the map on your location again.

Does anyone know how I can auto-rotate the map using maps API v2 just like the google maps app does? Will I have to implement this functionality myself or is it in the API and i'm just not seeing it? I appreciate all help.

like image 365
aleph_null Avatar asked Jan 14 '13 14:01

aleph_null


People also ask

How do I get Google Maps to auto-rotate on Android?

Enable Auto-Rotate You can enable it on Android by bringing down the Quick Settings panel and tapping on the Auto-rotate option. Likewise, iPhone users can also disable portrait orientation lock from the Control Center. To do that, swipe down from the top right corner of the screen and tap on the Rotation Lock button.

Can Google Maps go landscape?

All devices also have the ability to display anything on the screen in landscape mode or "side view." If you want to view your Android Maps application sideways, you can do so by taking advantage of the device's built-in orientation detector.


1 Answers

Ok here's how I think it should be done a year later. Please correct me if you spot any issues.

Most of the following code deals with a discrepancy between coordinate systems. I'm using a rotation vector sensor. From the docs: Y is tangential to the ground at the device's current location and points towards magnetic north. Bearing in google maps, on the other hand, seems to point to true north. this page shows how the conversion is done

1) get the current declination from your current GPS location

@Override public void onLocationChanged(Location location) {     GeomagneticField field = new GeomagneticField(             (float)location.getLatitude(),             (float)location.getLongitude(),             (float)location.getAltitude(),             System.currentTimeMillis()         );      // getDeclination returns degrees     mDeclination = field.getDeclination(); }  

2) calculate bearing from declination and magnetic north

    @Override public void onSensorChanged(SensorEvent event) {     if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {         SensorManager.getRotationMatrixFromVector(                 mRotationMatrix , event.values);         float[] orientation = new float[3];         SensorManager.getOrientation(mRotationMatrix, orientation);         float bearing = Math.toDegrees(orientation[0]) + mDeclination;         updateCamera(bearing);       } } 

3) update maps

private void updateCamera(float bearing) {     CameraPosition oldPos = mMap.getCameraPosition();      CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing).build();             mMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos)); } 
like image 111
aleph_null Avatar answered Oct 06 '22 01:10

aleph_null