2017-06-19 164 views
0

我已经使用swift构建iOS应用程序。CLLocationManager始终崩溃并返回零值

我在我的应用程序中实现了核心位置和mapview。这是第一次,它可以顺利运行,没有任何问题(在模拟器或iPhone上)。

该应用程序可以正常工作,并可以从我的iPhone获取当前位置。但是,当我尝试在Xcode中添加GPX位置时(我想尝试在GPX文件中使用任何位置),每件事都会发生变化。添加GPX文件并将其选为模拟器位置后,我的应用程序始终崩溃,并且CLLocationManager始终返回nil值。

我认为这个问题只存在于模拟器中,但我确实发生在我的iPhone中。即使删除GPX文件后,问题仍然存在。

每当我想获得纬度和经度值时,我总是会得到'EXC_BAD_INSTRUCTION'。

这是我的代码:

let corLoc = CLLocationManager() 

    //let corLoc2 = CLLocationManager() 


    corLoc.delegate = self 
    let statusLoc = CLLocationManager.authorizationStatus() 
    if statusLoc == .notDetermined{ 
     corLoc.requestWhenInUseAuthorization() 
    } 
    corLoc.desiredAccuracy = kCLLocationAccuracyBest 

    corLoc.startUpdatingLocation() 

    let lokasiAwal = CLLocationCoordinate2D(latitude: (corLoc.location?.coordinate.latitude)!, longitude: (corLoc.location?.coordinate.longitude)!) //<--- always return EXC_BAD_INSTRUCTION 


    let lokasiAkhir = CLLocationCoordinate2D(latitude: -7.299356, longitude: 112.676108) 

为您的信息,以前的应用程序中使用此代码正常工作

请帮我

PS:这是我的完整代码

class LocationViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 


@IBOutlet weak var mapRoute: MKMapView! 
var lokasiAwal2 = CLLocation() 



func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let userLocation:CLLocation = locations[0] as CLLocation 
    // manager.stopUpdatingLocation() 
    lokasiAwal2 = userLocation 

} 

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 

    if status == CLAuthorizationStatus.authorizedWhenInUse || status == CLAuthorizationStatus.authorizedAlways { 

     manager.startUpdatingLocation() 

    } 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    mapRoute.delegate = self 

    let corLoc = CLLocationManager() 

    //let corLoc2 = CLLocationManager() 


    corLoc.delegate = self 
    let statusLoc = CLLocationManager.authorizationStatus() 
    if statusLoc == .notDetermined{ 
     corLoc.requestWhenInUseAuthorization() 
    } 
    corLoc.desiredAccuracy = kCLLocationAccuracyBest 

    corLoc.startUpdatingLocation() 

    //let lokasiAwal = CLLocationCoordinate2D(latitude: (corLoc.location?.coordinate.latitude)!, longitude: (corLoc.location?.coordinate.longitude)!) 

    let lokasiAwal = CLLocationCoordinate2D(latitude: lokasiAwal2.coordinate.latitude, longitude: lokasiAwal2.coordinate.longitude) 


    //let lokasiAwal = CLLocationCoordinate2D(latitude: -7.263056, longitude: 112.740317) 

    let lokasiAkhir = CLLocationCoordinate2D(latitude: -7.299356, longitude: 112.676108) 
    //-7.299356, 112.676108 NH 
    //-7.289182, 112.676104 PTC 
    //-7.282713, 112.687633 bandar jakarta 
    //-7.263056, 112.740317 TP 

    //placemark 
    let awalPlaceMark = MKPlacemark(coordinate: lokasiAwal, addressDictionary: nil) 
    let akhirPlaceMark = MKPlacemark(coordinate: lokasiAkhir, addressDictionary: nil) 

    let awalMap = MKMapItem(placemark: awalPlaceMark) 
    let akhirMap = MKMapItem(placemark: akhirPlaceMark) 


    //anotasi 
    let awalAnotasi = MKPointAnnotation() 
    awalAnotasi.title = "Your Location" 


    //let awalPin = MKPinAnnotationView.init(annotation: awalAnotasi, reuseIdentifier: "Your Location") 
    //awalPin.pinTintColor = UIColor.blue 


    if let locationAwal = awalPlaceMark.location { 
     awalAnotasi.coordinate = locationAwal.coordinate 
    } 

    let akhirAnotasi = MKPointAnnotation() 
    akhirAnotasi.title = "National Hospital" 

    if let locationAkhir = akhirPlaceMark.location { 
     akhirAnotasi.coordinate = locationAkhir.coordinate 
    } 

    let awalPin = MyPointAnnotation() 
    awalPin.coordinate = awalAnotasi.coordinate 
    awalPin.pinTintColor = .green 
    awalPin.title = awalAnotasi.title 

    let akhirPin = MyPointAnnotation() 
    akhirPin.coordinate = akhirAnotasi.coordinate 
    akhirPin.pinTintColor = .blue 
    akhirPin.title = akhirAnotasi.title 

    //titik marker 
    self.mapRoute.showAnnotations([awalPin, akhirPin], animated: true) 


    //menambahkan route 
    let directionRequest = MKDirectionsRequest() 
    directionRequest.source = awalMap 
    directionRequest.destination = akhirMap 
    directionRequest.transportType = .automobile 

    let directions = MKDirections(request: directionRequest) 
    directions.calculate 
     { 
      (response, error) -> Void in 

      guard let response = response else 
      { 
       if let error = error { 
        print("Error : \(error)") 
       } 
       return 
      } 

      let route = response.routes[0] 


      self.mapRoute.add((route.polyline), level: MKOverlayLevel.aboveRoads) 

      let rect = route.polyline.boundingMapRect 
      self.mapRoute.setRegion(MKCoordinateRegionForMapRect(rect), animated: true) 
      self.mapRoute.delegate = self 

    } 
    // Do any additional setup after loading the view. 
} 

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    let renderer = MKPolylineRenderer(overlay: overlay) 
    renderer.lineWidth = 1.0 
    renderer.strokeColor = UIColor.red 

    return renderer 
} 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    var annotView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView 
    if annotView == nil { 
     annotView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation") 
    } 
    else { 
     annotView?.annotation = annotation 
    } 
    if let annotation = annotation as? MyPointAnnotation { 
     annotView?.pinTintColor = annotation.pinTintColor 
     annotView?.canShowCallout = true 
    } 

    return annotView 
} 



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


/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 

}

这是我的一点注记类

class MyPointAnnotation : MKPointAnnotation { 
var pinTintColor: UIColor? 

}

+0

为什么不执行''didUpdateLocations' CLLocationManagerDelegate'method? –

+0

以前它可以在不didUpdateLocations流畅运行 – christ2702

+0

我已经实现didupdatelocations,但还是给了错误 – christ2702

回答

0

正如我在我的评论说:你的问题是,CLLocationManager也许鸵鸟政策有任何的位置,但,这样你就迫使那些可能无解包的价值观,在didUpdateLocations这不会再发生了,因为这种方法当CLLocationManager定义位置称为

在你的代码的主要变化是

extension LocationViewController : CLLocationManagerDelegate 
    { 
     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

      if let location = locations.last 
      { 
       if((location.horizontalAccuracy) < CLLocationAccuracy(0)) 
       { 
        return 
       } 

       lokasiAwal2 = location 

       //Calling the method when we are sure that a position is getted 
       self.updateUIAndGetDirection() 
       self.corLoc.stopUpdatingLocation() //avoiding continue direction changes 
      } 
     } 
    } 

完整的C颂

import UIKit 
import CoreLocation 
import MapKit 

class LocationViewController: UIViewController { 


    @IBOutlet weak var mapRoute: MKMapView! 
    var lokasiAwal2 = CLLocation() 
    var corLoc = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     mapRoute.delegate = self 

     //let corLoc2 = CLLocationManager() 


     corLoc.delegate = self 
     let statusLoc = CLLocationManager.authorizationStatus() 
     if statusLoc == .notDetermined{ 
      corLoc.requestWhenInUseAuthorization() 
     } 
     corLoc.desiredAccuracy = kCLLocationAccuracyBest 

     corLoc.startUpdatingLocation() 


     // Do any additional setup after loading the view. 
    } 


    func updateUIAndGetDirection() 
    { 
     //let lokasiAwal = CLLocationCoordinate2D(latitude: (corLoc.location?.coordinate.latitude)!, longitude: (corLoc.location?.coordinate.longitude)!) 

     let lokasiAwal = CLLocationCoordinate2D(latitude: lokasiAwal2.coordinate.latitude, longitude: lokasiAwal2.coordinate.longitude) 


     //let lokasiAwal = CLLocationCoordinate2D(latitude: -7.263056, longitude: 112.740317) 

     let lokasiAkhir = CLLocationCoordinate2D(latitude: -7.299356, longitude: 112.676108) 
     //-7.299356, 112.676108 NH 
     //-7.289182, 112.676104 PTC 
     //-7.282713, 112.687633 bandar jakarta 
     //-7.263056, 112.740317 TP 

     //placemark 
     let awalPlaceMark = MKPlacemark(coordinate: lokasiAwal, addressDictionary: nil) 
     let akhirPlaceMark = MKPlacemark(coordinate: lokasiAkhir, addressDictionary: nil) 

     let awalMap = MKMapItem(placemark: awalPlaceMark) 
     let akhirMap = MKMapItem(placemark: akhirPlaceMark) 


     //anotasi 
     let awalAnotasi = MKPointAnnotation() 
     awalAnotasi.title = "Your Location" 


     //let awalPin = MKPinAnnotationView.init(annotation: awalAnotasi, reuseIdentifier: "Your Location") 
     //awalPin.pinTintColor = UIColor.blue 


     if let locationAwal = awalPlaceMark.location { 
      awalAnotasi.coordinate = locationAwal.coordinate 
     } 

     let akhirAnotasi = MKPointAnnotation() 
     akhirAnotasi.title = "National Hospital" 

     if let locationAkhir = akhirPlaceMark.location { 
      akhirAnotasi.coordinate = locationAkhir.coordinate 
     } 

     let awalPin = MyPointAnnotation() 
     awalPin.coordinate = awalAnotasi.coordinate 
     awalPin.pinTintColor = .green 
     awalPin.title = awalAnotasi.title 

     let akhirPin = MyPointAnnotation() 
     akhirPin.coordinate = akhirAnotasi.coordinate 
     akhirPin.pinTintColor = .blue 
     akhirPin.title = akhirAnotasi.title 

     //titik marker 
     self.mapRoute.showAnnotations([awalPin, akhirPin], animated: true) 


     //menambahkan route 
     let directionRequest = MKDirectionsRequest() 
     directionRequest.source = awalMap 
     directionRequest.destination = akhirMap 
     directionRequest.transportType = .automobile 

     let directions = MKDirections(request: directionRequest) 
     directions.calculate 
      { 
       (response, error) -> Void in 

       guard let response = response else 
       { 
        if let error = error { 
         print("Error : \(error)") 
        } 
        return 
       } 

       let route = response.routes[0] 


       self.mapRoute.add((route.polyline), level: MKOverlayLevel.aboveRoads) 

       let rect = route.polyline.boundingMapRect 
       self.mapRoute.setRegion(MKCoordinateRegionForMapRect(rect), animated: true) 
       self.mapRoute.delegate = self 

     } 
    } 


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


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
    } 
    */ 
} 

extension LocationViewController : MKMapViewDelegate 
{ 
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
     let renderer = MKPolylineRenderer(overlay: overlay) 
     renderer.lineWidth = 1.0 
     renderer.strokeColor = UIColor.red 

     return renderer 
    } 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

     var annotView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView 
     if annotView == nil { 
      annotView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation") 
     } 
     else { 
      annotView?.annotation = annotation 
     } 
     if let annotation = annotation as? MyPointAnnotation { 
      annotView?.pinTintColor = annotation.pinTintColor 
      annotView?.canShowCallout = true 
     } 

     return annotView 
    } 

} 

extension LocationViewController : CLLocationManagerDelegate 
{ 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     if let location = locations.last 
     { 
      if((location.horizontalAccuracy) < CLLocationAccuracy(0)) 
      { 
       return 
      } 

      lokasiAwal2 = location 

      self.updateUIAndGetDirection() 
      self.corLoc.stopUpdatingLocation() //avoiding continue direction changes 
     } 
    } 

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 

     if status == CLAuthorizationStatus.authorizedWhenInUse || status == CLAuthorizationStatus.authorizedAlways { 

      manager.startUpdatingLocation() 

     } 
    } 
} 

希望这有助于

+0

仍然给予零结果 – christ2702

+0

我觉得有什么不对的一套模拟在我前面提到的位置。因为,此前@ christ2702我的回答与你的代码更新固定我的应用程序可以正常工作 – christ2702

+0

,我希望这可以帮助你 –

0

使用CoreLocation代表在viewdidload &得到didUpdateLocations

import UIKit 
import CoreLocation 

class ViewController: UIViewController,CLLocationManagerDelegate { 

    let corLoc = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     corLoc.delegate = self 
     let statusLoc = CLLocationManager.authorizationStatus() 
     if statusLoc == .notDetermined{ 
      corLoc.requestWhenInUseAuthorization() 
     } 
     corLoc.desiredAccuracy = kCLLocationAccuracyBest 
     corLoc.startUpdatingLocation() 

    } 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     print((corLoc.location?.coordinate.latitude) ?? "No values") 
     let lokasiAwal = CLLocationCoordinate2D(latitude: (corLoc.location?.coordinate.latitude)!, longitude: (corLoc.location?.coordinate.longitude)!) // 
     let lokasiAkhir = CLLocationCoordinate2D(latitude: -7.299356, longitude: 112.676108) 
    } 

} 
+0

还是一样的结果 – christ2702

+0

就像我之前提到的,我认为这个问题是在模拟位置 – christ2702

+0

伙计,我试图及其工作的罚款。如果你是在模拟器选择位置手动。 – Jack

0

坐标您的问题是CLLocationManager也许鸵鸟政策有任何的位置,但。代表方法didUpdateLocations将在数据准备就绪时调用。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let userLocation:CLLocation = locations[0] as CLLocation 
    // manager.stopUpdatingLocation() 
    lokasiAwal = userLocation 
} 
+0

它仍然给无所获 – christ2702