Set a max zoom level on LatLngBounds builder

前端 未结 10 631
慢半拍i
慢半拍i 2020-12-13 00:57

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

10条回答
  •  遥遥无期
    2020-12-13 01:25

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

提交回复
热议问题