-2

I'm trying to include a map in my application - following the instructions here. The problem is that I'm getting an error when setting the region - the line of code "let region = MKMap..." and I don't know why. Has anyone encountered this problem or know how to help? Thanks!

Code update:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate {

    // MARK: Properties
    @IBOutlet weak var MapView: MKMapView!
    var locationManager = CLLocationManager.init()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        locationManager.requestWhenInUseAuthorization()

        MapView.mapType = .standard
        MapView.showsUserLocation = true
        MapView.showsScale = true
        MapView.showsCompass = true

        let span = MKCoordinateSpan.init(latitudeDelta: 0.0075, longitudeDelta: 0.0075)
        let region = MKCoordinateRegion.init(center: (locationManager.location?.coordinate)!, span: span)
        MapView.setRegion(region, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


} 

Error update: The line that causes the error is:

let region = MKCoordinateRegion.init(center: (locationManager.location?.coordinate)!, span: span)

and the error is:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
user282041
  • 37
  • 8
  • Update your question with relevant code (and clearly show the line causing the error) and the complete, exact error message. – rmaddy Dec 03 '17 at 19:30
  • It makes no sense to use optional chaining if you just force unwrap the value afterwards... It's quite obvious where your issue is coming from, handle the optionals properly, don't use force unwrapping on optionals that might be `nil`. Also don't call `init` explicitly, just use the shorthand notation `MKCoordinateRegion(center:span:)`. – Dávid Pásztor Dec 03 '17 at 20:04
  • I'm sorry, I'm new to iOS and Swift programming. The link in my original post provided me a walk-through guide, while other links on Stack Overflow just provided code snippets that I was unsure of using. So you would recommend removing the `.init` from `MKCoordinateRegion` and the `?` from the `locationManager.location?` ? – user282041 Dec 03 '17 at 20:13
  • In the phrase `(locationManager.location?.coordinate)!`, the exclamation mark at the end _means_ "crash me". You cannot complain if that is exactly what happens. – matt Dec 03 '17 at 20:40

1 Answers1

1

Your code will always crash. You are saying:

    locationManager.requestWhenInUseAuthorization()
    // ...
    let region = MKCoordinateRegion.init(center: (locationManager.location?.coordinate)!, span: span)

So you have given the location manager no instructions to start getting the device's location, but here you are force-unwrapping the location manager's location. Naturally, that location is nil, and naturally you crash.

If your goal is to have the map show the user's location, then set the map's showsUserLocation or (better) its userTrackingMode and stop. Just stand back and let the map do its work. Do not fight the map view by trying to dictate its region; it knows better than you do how to display the user's location.

matt
  • 515,959
  • 87
  • 875
  • 1,141