0

我正在尝试制作一个查看我要去的大学的应用程序,但我无法仅查看一个城市。我试图确保用户不能滚动过去的城市。然后我试图覆盖那个区域。我认为 setRegion 方法将有助于解决该问题,但显然不是。关于如何设置用户无法超越的区域有什么建议吗?

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    // sets maps to univeristy
    var location = CLLocationCoordinate2DMake(42.9633,
        -85.890042)
    // Span and region
    var span = MKCoordinateSpanMake (0.005, 0.005)
    var region = MKCoordinateRegion(center: location, span: span)
    Map.setRegion(region, animated: true)
4

1 回答 1

1

我将此处找到的 Obj-C 代码翻译成 Swift : https ://gist.github.com/Alp-Phone/e11cca67e77285566d4d链接死了。

lazy var restrictedRegion: MKCoordinateRegion = {
    // sets maps to univeristy
    let location = CLLocationCoordinate2DMake(42.9633, -85.890042)
    // Span and region
    let span = MKCoordinateSpanMake (0.005, 0.005)
    return MKCoordinateRegion(center: location, span: span)
}()

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.setRegion(restrictedRegion, animated: true)
}

var manuallyChangingMap = false //Stop from updating while animating
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    if !manuallyChangingMap && ((mapView.region.span.latitudeDelta > restrictedRegion.span.latitudeDelta * 4) ||
            (mapView.region.span.longitudeDelta > restrictedRegion.span.longitudeDelta * 4) ||
            fabs(mapView.region.center.latitude - restrictedRegion.center.latitude) > restrictedRegion.span.latitudeDelta ||
            fabs(mapView.region.center.longitude - restrictedRegion.center.longitude) > restrictedRegion.span.longitudeDelta) {

        manuallyChangingMap = true
        mapView.setRegion(restrictedRegion, animated: true)
        manuallyChangingMap = false
    }
}
于 2015-08-04T02:14:18.873 回答