Swift- MKMapkit仅查看一个城市? [英] Swift- MKMapkit view only one city?

查看:96
本文介绍了Swift- MKMapkit仅查看一个城市?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序来查看我要去的大学,但仅在查看一个城市时遇到了麻烦.我正在尝试确保用户不能滚动到城市之外.然后,我尝试覆盖该区域.我认为setRegion方法将有助于解决该问题,但显然没有.关于如何设置用户无法超越的区域的任何建议?

I'm trying to make an app viewing the college I'm going to but I'm having trouble only viewing the one city. I'm trying to make sure that the user cannot scroll past the city. I'm then trying to overlay that region. I thought the setRegion method would help fix that issue but apparently not. Any suggestions on how to set the region in which the user cannot surpass?

    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)

推荐答案

我翻译了在这里找到的Obj-C代码:

I translated the Obj-C code found here: https://gist.github.com/Alp-Phone/e11cca67e77285566d4d to Swift Link is dead.

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

这篇关于Swift- MKMapkit仅查看一个城市?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆