【发布时间】:2017-11-21 04:25:32
【问题描述】:
我已经使用 swift 构建了一个 iOS 应用程序。
我在我的应用程序中实现了核心位置和地图视图。第一次,它可以毫无问题地顺利运行(在模拟器或 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?
}
【问题讨论】:
-
为什么不实现
didUpdateLocationsCLLocationManagerDelegate方法? -
以前不用didUpdateLocations也能流畅运行
-
我已经实现了didupdatelocations,但还是报错
-
你的问题是
CLLocationManager可能还没有任何位置,所以你强制 unwrap 可能是 nil 的值,在didUpdateLocations这不会再发生了,因为这个方法被调用当CLLocationManager定义位置时 -
你能发布你的这个viewController的全部代码吗?你需要在
didUpdateLocations中设置你的lokasiAwal值一旦CLLocationManager获得有效位置
标签: ios swift xcode core-location cllocationmanager