如何根据 Places API 返回的结果相应地缩放 Google 地图?

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

我目前正在努力实现流畅的缩放动画,Google 地图有,例如,如果用户搜索城市,地图缩放得足够大,所以他可以看到整个城市,如果他搜索一个国家,缩放比例会大得多。我目前已经达到获取 Place 对象及其 LatLng 数据的地步,但还没有找到一种方法来为给定的搜索正确缩放。

据我所知,应该通过向 RectangularBounds 对象提供西南点和东北点并将其设置到地图来完成,但还没有找到使用 Android API

您应该使用 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() 方法。类似的东西:

// 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);
}