0

I am new in android, I am making application which have Google Map V2. I want to animateCamera Zoom value at runtime. It should be depend on the current location(latitude and longitude) and Destination Location(destination latitude and longitude).

It should be Zoom such it should show both marker as nearest possible and should not require any pan, zoom in/out gesture on mapv2.

I used this code but I need to pan gesture to see both market on the Map. I want it should be default as nearest possible and should not require any pan gesture.

map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));

Thanks in advance.

user2601652
  • 467
  • 2
  • 8
  • 26

1 Answers1

1

you need to use newLatLngBounds()

Returns a CameraUpdate that transforms the camera such that the specified latitude/longitude bounds are centered on screen at the greatest possible zoom level. You can specify padding, in order to inset the bounding box from the map view's edges. The returned CameraUpdate has a bearing of 0 and a tilt of 0.

figure out the bounding box of the 2 points and give the southwest most point and the north east most point

example:

map.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southWest,northEast),10));

to get the bounding box you need to loop through your points and find the max/min lat/lng

example:

for(//for loop){
    LatLng point = new LatLng(lat,lng)

    if(point.getLatitude() > maxLat){
        maxLat = point.getLatitude();
    }
    if(point.getLatitude() < minLat){
        minLat = point.getLatitude();
    }
    if(point.getLongitude() > maxLon){
        maxLon = point.getLongitude();
    }
    if(point.getLongitude() < minLon){
        minLon = point.getLongitude();
    }
}

northEast = new LatLng(maxLat,maxLon);
southWest = new LatLng(minLat,minLon);
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • How to get southWest,northEast?. I have four coordinate current location and destination location latitude and longitudes – user2601652 Aug 08 '13 at 18:00
  • Sorry, I am still unable to understand the code. Use of for loop and what to maxLat, minLat, maxlon, minLon initially – user2601652 Aug 08 '13 at 18:13
  • just loop through your 2 points and find the maximum latitude, minimum latitude, maximum longitude and minimum longitude – tyczj Aug 08 '13 at 18:15
  • I am getting this exception 08-09 00:16:16.554: E/AndroidRuntime(28998): Caused by: java.lang.IllegalStateException: Map size should not be 0. Most likely, layout has not yet occured for the map view. – user2601652 Aug 08 '13 at 18:47
  • you are trying to move the map before it is being shown, I had the same problem. check my question here for the suggested solution, you basically need to wait until the view loads to move the map http://stackoverflow.com/questions/17820617/illegalstateexception-map-size-should-not-be-0 – tyczj Aug 08 '13 at 18:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35094/discussion-between-user2601652-and-tyczj) – user2601652 Aug 08 '13 at 19:02