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
Have you considered making a map fragment that is hidden, and using that to determine your zoom/bounds? It's obviously not the best computationally, but it's probably the easiest/most accurate solution. For instance, it's complex to make a mathematical solution that works near the poles and across 0 degrees longitude (if you care). If you use a map, that's all built in already. You could also obviously use this solution on your existing map, but the user would see a bounce.
Here's how you'd do it:
Here is a much shorter version based on user2808624's answer.
LatLngBounds bounds = builder.build();
LatLng center = bounds.getCenter();
builder.include(new LatLng(center.latitude-0.001f,center.longitude-0.001f));
builder.include(new LatLng(center.latitude+0.001f,center.longitude+0.001f));
bounds = builder.build();
Its a bit sloppy, but you can use 0.001 to zoom no less than a city block, 0.01 for no less than a city view. Only advantage is its only 4 extra lines of code.
Quick fix is:
gMap.setOnMapLoadedCallback(this);
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
gMap.setMaxZoomPreference(10);
gMap.animateCamera(cu);
@Override
public void onMapLoaded() {
gMap.resetMinMaxZoomPreference();
}
Beware that it could halt zoom until map is fully reloaded. But as a quick fix it works.
I have a similar case, where I want at least a map diagonal of 2 kilometers. Therefore I just add two additional points to the builder: the first 1 km north-west of the center of the original bounds and the second 1 km south-east of the center. If these are inside the bounds, they do not change anything. If they are outside of the original bounds, they increase the bounds to a meaningful size.
Here is a full code example:
public LatLngBounds createBoundsWithMinDiagonal(MarkerOptions firstMarker, MarkerOptions secondMarker) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(firstMarker.getPosition());
builder.include(secondMarker.getPosition());
LatLngBounds tmpBounds = builder.build();
/** Add 2 points 1000m northEast and southWest of the center.
* They increase the bounds only, if they are not already larger
* than this.
* 1000m on the diagonal translates into about 709m to each direction. */
LatLng center = tmpBounds.getCenter();
LatLng northEast = move(center, 709, 709);
LatLng southWest = move(center, -709, -709);
builder.include(southWest);
builder.include(northEast);
return builder.build();
}
private static final double EARTHRADIUS = 6366198;
/**
* Create a new LatLng which lies toNorth meters north and toEast meters
* east of startLL
*/
private static LatLng move(LatLng startLL, double toNorth, double toEast) {
double lonDiff = meterToLongitude(toEast, startLL.latitude);
double latDiff = meterToLatitude(toNorth);
return new LatLng(startLL.latitude + latDiff, startLL.longitude
+ lonDiff);
}
private static double meterToLongitude(double meterToEast, double latitude) {
double latArc = Math.toRadians(latitude);
double radius = Math.cos(latArc) * EARTHRADIUS;
double rad = meterToEast / radius;
return Math.toDegrees(rad);
}
private static double meterToLatitude(double meterToNorth) {
double rad = meterToNorth / EARTHRADIUS;
return Math.toDegrees(rad);
}