Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android maps V2 newLatLngBounds with bearing

Tags:

I'm using the new Android maps V2 with this layout:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:map="http://schemas.android.com/apk/res-auto"   android:id="@+id/map"   android:layout_width="match_parent"   android:layout_height="match_parent"   class="com.google.android.gms.maps.SupportMapFragment"   map:cameraBearing="270"/> 

I'm trying to use the method newLatLngBounds (LatLngBounds bounds, int padding) for zooming and seeing all my markers in the map, however the camera bearing is set to 0.

The description on google developers documentation says:

public static CameraUpdate newLatLngBounds (LatLngBounds bounds, int padding) (...) . The returned CameraUpdate has a bearing of 0 and a tilt of 0. (...)".

How can I change the bearing value?

I tried to set a new bearing programmatically after the call of newLatLngBounds, something like this:

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)); mMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder() .target(mMap.getCameraPosition().target) .zoom(mMap.getCameraPosition().zoom) .bearing(270) .build())); 

But when I do this some markers won't show up.

like image 716
pedroca Avatar asked Jan 31 '13 17:01

pedroca


1 Answers

I've got an alternative, I had the same issue. What I'll show is how to convert a bounds to a zoom, and then we'll simply use a CameraPosition.builder() to create the right position.

private static final double LN2 = 0.6931471805599453;     private static final int WORLD_PX_HEIGHT = 256;     private static final int WORLD_PX_WIDTH = 256;     private static final int ZOOM_MAX = 21;      public int getBoundsZoomLevel(LatLngBounds bounds, int mapWidthPx, int mapHeightPx){          LatLng ne = bounds.northeast;         LatLng sw = bounds.southwest;          double latFraction = (latRad(ne.latitude) - latRad(sw.latitude)) / Math.PI;          double lngDiff = ne.longitude - sw.longitude;         double lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;          double latZoom = zoom(mapHeightPx, WORLD_PX_HEIGHT, latFraction);         double lngZoom = zoom(mapWidthPx, WORLD_PX_WIDTH, lngFraction);          int result = Math.min((int)latZoom, (int)lngZoom);         return Math.min(result, ZOOM_MAX);     }      private double latRad(double lat) {         double sin = Math.sin(lat * Math.PI / 180);         double radX2 = Math.log((1 + sin) / (1 - sin)) / 2;         return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;     }     private double zoom(int mapPx, int worldPx, double fraction) {         return Math.floor(Math.log(mapPx / worldPx / fraction) / LN2);     } 

Then you can use this code and add a bearing/and or tilt with the bounds:

            LatLngBounds bounds = LatLngBounds.builder()                     .include(swCorner)                     .include(neCorner).build();             CameraPosition cp = new CameraPosition.Builder()                     .bearing(randomBearing())                     .tilt(randomTilt())                     .target(latLng)                     .zoom(getBoundsZoomLevel(bounds, findViewById(R.id.map).getMeasuredWidth(), findViewById(R.id.map).getMeasuredHeight()))                     .build();             CameraUpdate cu = CameraUpdateFactory.newCameraPosition(cp);             mMap.animateCamera(cu); 

This will work (you'll have to change mMap to your actually map variable and the id to your map id).

Good luck! I got the boundZoomLevel from post: How do I determine the zoom level of a LatLngBounds before using map.fitBounds? Go like that answer too if you have time!

like image 76
Jono Avatar answered Oct 10 '22 18:10

Jono