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.
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.
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.
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With