Dimitar Filipov
Dimitar Filipov

Reputation: 39

How to zoom Google Map accordingly on a returned result from Places API?

I am currently trying to achieve to fluent zoom animations, that Google Maps has, where if the user searches for a city, for example, the map zooms enough, so he can see the entire city, and if he searches for a country the zoom is much larger. I have currently reached the point of getting the Place object, with its LatLng data, but have not found a way to zoom properly for a given search.

As far as I found out it should be done by supplying a southwest and northeast point to a RectangularBounds object and setting it to the map, but have not found a way to accomplish this with the Android API

Upvotes: 1

Views: 229

Answers (1)

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13343

You should use Place.getViewport() method from Places SDK for Android to get a viewport ( LatLngBounds) of a size that is suitable for displaying and then use it in animateCamera() or moveCamera() methods. Something like that:

// get place from places API response
Place place = places.get(0);

int padding = 0; // padding  in pixels
LatLngBounds = bounds place.getViewport();
if (bounds != null) {
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    googleMap.animateCamera(cameraUpdate);
}

Upvotes: 1

Related Questions