I haven\'t found an answer in my search, there are a few answers on SO but they didn\'t work for me.
I have 2 markers on the map and I am using LatLngBounds builder
Google has added an api for limiting max/min zoom levels.
GoogleMap.setMaxZoomPreference()
GoogleMap.setMinZoomPreference()
There is no way to limit zoom levels as of 4.0.30, but you may track this issue.
The easy way would be to force zoom level like on this video: http://www.youtube.com/watch?v=-nCZ37HdheY
Basically they are calling animateCamera
when certain zoom level is reached.
That would look a bit weird, because you then have 2 animations that are not started by the user.
The hard way would be doing the math yourself. Try working with CameraUpdateFactory.newLatLngZoom
with LatLng
as a center between these points and your max zoom when you detect points are close to each other.
I implemented this solution according to the answer by user2808624, but using the SphericalUtil from the Android Maps Utility Library to do the calculations.
final double HEADING_NORTH_EAST = 45;
final double HEADING_SOUTH_WEST = 225;
LatLng center = tmpBounds.getCenter();
LatLng northEast = SphericalUtil.computeOffset(center, 709, HEADING_NORTH_EAST);
LatLng southWest = SphericalUtil.computeOffset(center, 709, HEADING_SOUTH_WEST);
It is just simple:
LatLngBounds.Builder latlngBuilder = new LatLngBounds.Builder();
LatLng sydney1 = new LatLng(57.149651, -2.099075);
LatLng sydney2 = new LatLng(51.621441, -3.943646);
latlngBuilder.include(sydney1);
latlngBuilder.include(sydney2);
googlemap.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBuilder.build(), 100));
Coming late but maybe someone has the same issue I had: I received bounds from a Place, not from a LatLngBounds.Builder:
I used Location.distanceBetween(...) to find out if the bounds were too small, and then use a minimum zoom level with the center of the bounds:
if (areBoundsTooSmall(bounds, 300)) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 17));
} else {
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
}
The check method is:
private boolean areBoundsTooSmall(LatLngBounds bounds, int minDistanceInMeter) {
float[] result = new float[1];
Location.distanceBetween(bounds.southwest.latitude, bounds.southwest.longitude, bounds.northeast.latitude, bounds.northeast.longitude, result);
return result[0] < minDistanceInMeter;
}
I used min zoom level 17 and padding 20 for bounds that are too small, too small is definded here by min distance in meter 300. You can adjust the numbers as you need.
Following from the answers by user2808624 and dvdrlee, I wrote an extension in Kotlin:
fun LatLngBounds.Builder.createBoundsWithZoomMaintained(bounds: LatLngBounds) : LatLngBounds {
val HEADING_NORTH_EAST = 45.0 //NorthEast angle
val HEADING_SOUTH_WEST = 215.0 //SouthWest angle
val center = bounds.center
val northEast = SphericalUtil.computeOffset(center, 709.0, HEADING_NORTH_EAST) //1000 metres on the diagonal translates to 709 metres on either side.
val southWest = SphericalUtil.computeOffset(center, 709.0, HEADING_SOUTH_WEST)
this.include(northEast).include(southWest)
return this.build()
}
This would typically be in your Utils or Extensions file, and then you can use it from anywhere simply like this:
var bounds = builder.build() //To include all your existing points.
bounds = builder.createBoundsWithZoomMaintained(bounds) //Updated bounds.
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10))