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

标签 ios swift google-maps

我的一位同事正在 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)

但我们没有在 fitBoundsanimateWithCameraUpdate 上找到任何缩放控件

最佳答案

我找到了一个简单的技巧来解决这个问题。您可以在 fitanimate 之前使用 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/

相关文章:

ios - 阻止 UIView 旋转为横向

macos - 如何设置 NSPathComponentCell 的 URL?

Android - 谷歌地图显示位置名称而不是经度和纬度

objective-c - 无法将 objective-c 行转换为 swift

即使设置为 "never",也可以访问 iOS11 照片库

google-maps - 从 google map api 获取评论

android - Android 中用户当前位置图标覆盖的聚类标记

ios - 无法在文档目录的子文件夹中创建 sqlite 数据库

ios - 如何区分本地通知

ios - 如何自定义单元格的一部分