我的一位同事正在 ios-swift 上使用 google map,她想显示一些标记来绘制 map 并在第一时间适应 map 缩放以仅显示所有标记。 主要问题发生在标记靠得很近并且 map 缩放到 18 或 19 级时太多了。她想防止这种情况,在这种情况下,将 map 缩放设置为 15 级,但是之后显示,用户可以根据需要放大标记。我们知道可以使用下面的代码片段将 map 拟合到标记
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)
但我们没有在 fitBounds
或 animateWithCameraUpdate
上找到任何缩放控件
最佳答案
我找到了一个简单的技巧来解决这个问题。您可以在 fit
和 animate
之前使用 setMinZoom
来防止过度缩放,然后再次使用 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
关于ios - swift - 如何适合 GMSMapView(谷歌地图)以显示所有标记并防止过度缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53358418/