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
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))