线程1:获取纬度和经度时出现EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)错误

时间:2016-03-15 14:41:12

标签: ios swift

请帮我解决这个问题

import Foundation  
import UIKit 
import MapKit

class DetailViewController : UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    var selectedLocation : LocationModel?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        // Create coordinates from location lat/long
        var poiCoodinates: CLLocationCoordinate2D = CLLocationCoordinate2D()

        poiCoodinates.latitude = CDouble(self.selectedLocation!.latitude!)! //Problem is in this line
        poiCoodinates.longitude = CDouble(self.selectedLocation!.longitude!)!
        // Zoom to region
        let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750)
        self.mapView.setRegion(viewRegion, animated: true)
        // Plot pin
        let pin: MKPointAnnotation = MKPointAnnotation()
        pin.coordinate = poiCoodinates
        self.mapView.addAnnotation(pin)

        //add title to the pin
        pin.title = selectedLocation!.name
    }

}

2 个答案:

答案 0 :(得分:1)

您尚未初始化var selectedLocation : LocationModel?,所以当您要求self.selectedLocation!崩溃时。

  

添加所需的初始化并尝试重构您的代码   方式:

override func viewDidAppear(animated: Bool) {
   guard let location = self.selectedLocation, let latitude = location.latitude, let longitude =  location.longitude else {
     return //Here was an error, so you can not continue, report it or do something about it before returning
   } 

    // Create coordinates from location lat/long
    var poiCoodinates: CLLocationCoordinate2D = CLLocationCoordinate2D()

    poiCoodinates.latitude = CDouble(latitude)! 
    poiCoodinates.longitude = CDouble(longitude)!
    // Zoom to region
    let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750)
    self.mapView.setRegion(viewRegion, animated: true)
    // Plot pin
    let pin: MKPointAnnotation = MKPointAnnotation()
    pin.coordinate = poiCoodinates
    self.mapView.addAnnotation(pin)

    //add title to the pin
    pin.title = location.name
}

答案 1 :(得分:0)

我终于找到了解决方案,就在这里:

导入MapKit

DetailViewController类:UIViewController,MKMapViewDelegate {

//var mapType:UISegmentedControl!
//var showPointsOfInterest:UISwitch!


@IBOutlet weak var mapView: MKMapView!

@IBAction func showDirection(sender: AnyObject) {
}

var selectedLocation : LocationModel?
let locationManager = CLLocationManager()
var currentPlacemark:CLPlacemark?
//var segmentedControlAciton:UISegmentedControl!

@IBAction func myLocation(sender: AnyObject) {
    // Request for a user's authorization for location services
    locationManager.requestWhenInUseAuthorization()
    let status = CLLocationManager.authorizationStatus()
    if status == CLAuthorizationStatus.AuthorizedWhenInUse {
        mapView.showsUserLocation = true


    }

}

//

override func viewDidLoad() {
    super.viewDidLoad()
   // mapView.showsUserLocation = true
    title = selectedLocation?.name

    mapView.delegate = self
}

override func viewDidAppear(animated: Bool) {
    // Create coordinates from location lat/long
    var poiCoordinates: CLLocationCoordinate2D = CLLocationCoordinate2D()

    poiCoordinates.latitude = CDouble(self.selectedLocation!.latitude!)!
    poiCoordinates.longitude = CDouble(self.selectedLocation!.longitude!)!
    // Zoom to region
    let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoordinates, 750, 750)
    self.mapView.setRegion(viewRegion, animated: true)
    // Plot pin
    let pin: MKPointAnnotation = MKPointAnnotation()
    pin.coordinate = poiCoordinates
    self.mapView.addAnnotation(pin)

    //add title to the pin
    pin.title = selectedLocation!.name 
    pin.subtitle=selectedLocation!.address
    mapView.showsScale = true



}