swift - 如何适应 GMSMapView(谷歌地图)以显示所有标记并防止过度缩放

Mil*_*adi 5 google-maps ios swift

我的一位同事正在 ios-swift 上使用谷歌地图,她想显示一些标记来映射并第一次放大地图以仅显示所有标记。当标记靠得太近,地图缩放到 18 或 19 级时会出现主要问题,而且太多了。她想防止这种情况,在这种情况下,将地图缩放设置为 15 级,但在显示后,用户可以根据需要放大标记。我们知道可以使用下面的代码段将地图与标记匹配

var bounds = GMSCoordinateBounds()
for location in locationArray
{
    let latitude = location.valueForKey("latitude")
    let longitude = location.valueForKey("longitude")

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude:latitude, longitude:longitude)
    marker.map = self.mapView
    bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fit(bounds, withPadding: 50)
mapView.animate(update)
Run Code Online (Sandbox Code Playgroud)

但我们没有发现任何缩放控制fitBoundsanimateWithCameraUpdate

Mil*_*adi 9

我找到了一个简单的技巧来解决这个问题。您可以使用setMinZoombeforefitanimate防止过度缩放,然后setMinZoom再次使用以允许用户缩放。

var bounds = GMSCoordinateBounds()
for location in locationArray
{
    let latitude = location.valueForKey("latitude")
    let longitude = location.valueForKey("longitude")

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude:latitude, longitude:longitude)
    marker.map = self.mapView
    bounds = bounds.includingCoordinate(marker.position)
}

mapView.setMinZoom(1, maxZoom: 15)//prevent to over zoom on fit and animate if bounds be too small

let update = GMSCameraUpdate.fit(bounds, withPadding: 50)
mapView.animate(update)

mapView.setMinZoom(1, maxZoom: 20) // allow the user zoom in more than level 15 again
Run Code Online (Sandbox Code Playgroud)