我有一款应用程序,上面有一张地图的预览版,在上面录制完后,全屏都会动起来:
由于mapView底部的半透明条,我在地图完成后调用mapView.setVisibleMapRect(mapView.visibleMapRect, edgePadding: UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0), animated: false)
来偏移地图的中心:
mapView.showsUserLocation = true
let regionRadius: CLLocationDistance = 700
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: regionRadius, longitudinalMeters: regionRadius)
mapView.setRegion(region, animated: true)
现在,我想扩展mapView
以填充整个屏幕。好的是,地图的缩放和位置保持原样,只有帧按预期展开( userPosition仍然居中,但不可见,因为它就在半透明面板的右侧,只需比较一下地图):
不好的是,我希望地图中心在蓝色盒子和半透明面板之间。问题是,当我试图偏移地图以显示用户在地图新可见区域中心的位置时。我需要再次使用函数setVisibleMapRect
(据我理解)来设置一个新的edgePadding。但是我测试的一切都随着地图的放大或缩小而结束,当我在扩展状态和崩溃状态之间跳跃时,我会越来越快地放大。
因此,我搜索一种方法来稍微偏移地图以正确显示用户的位置,比如mapView.setEdgePadding(UIEdgeInsets(...), animated: true)
或者我需要一种方法根据当前的缩放级别和新的帧计算正确的MKMapRect
,这样我就可以使用setVisibleMapRect
函数而不改变地图的缩放级别。
我想要的结果是:
发布于 2021-05-26 18:09:53
如果您希望用户更动态地以中心为中心,请考虑以下其中一个:
mapView.setUserTrackingMode(.followWithHeading, animated: animated)
mapView.setUserTrackingMode(.follow, animated: animated)
这可以改变缩放级别。
如果只想设置中心用户位置一次,请尝试:
var region = mapView.region
region.center = userLocation
mapView.setRegion(region, animated: true)
这不应更改缩放级别。
https://stackoverflow.com/questions/67709028
复制相似问题