Swift- MKMapkit view only one city?
Asked Answered
G

1

0

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)
Gump answered 4/8, 2015 at 1:43 Comment(2)
Asked here in Obj-C: #5681396Beverle
I'm having a really hard time understanding objective-C, sorry I'm a noob at this.Gump
B
1

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
    }
}
Beverle answered 4/8, 2015 at 2:14 Comment(2)
I'm getting an error saying " 'viewController' doesn't have a member named 'manuallyChangingMap' on line 2, and same error reappears every the 'self.' call is made.Gump
You need to add: var manuallyChangingMap = false in your ViewController. I'm assuming the person who originally wrote this func noticed that the func could be called again while it was animating the setRegion func. To combat this, he used a bool to let the func know that it was already updating the regionBeverle

© 2022 - 2024 — McMap. All rights reserved.