Swift- MKMapkit只查看一个城市?

时间:2015-08-04 01:43:15

标签: swift mapkit xcode7-beta2

我试图让应用程序查看我要去的大学,但我只是在查看这个城市时遇到了麻烦。我试图确保用户无法滚动浏览城市。然后,我试图覆盖该区域。我认为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)

1 个答案:

答案 0 :(得分:1)

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

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
    }
}