Ios 线程1:使用MKMap视图时的EXC_断点(代码=1,子代码=…)

Ios 线程1:使用MKMap视图时的EXC_断点(代码=1,子代码=…),ios,swift,xcode,Ios,Swift,Xcode,因此,我最近在MKMapView中遇到了一个问题。每当我点击一个按钮进入包含地图的视图时,我都会看到上面的错误围绕着这条线 更新了完整的脚本。错误为let region=MKCoordinateRegion.init(中心:(userLocation.location?.coordinate)!,纬度计:5000,经度计:5000) mapView.setRegion(区域,动画:true)} override func viewDidLoad() { super.viewDidLoad

因此,我最近在MKMapView中遇到了一个问题。每当我点击一个按钮进入包含地图的视图时,我都会看到上面的错误围绕着这条线

更新了完整的脚本。错误为
let region=MKCoordinateRegion.init(中心:(userLocation.location?.coordinate)!,纬度计:5000,经度计:5000)
mapView.setRegion(区域,动画:true)}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    mapView.showsUserLocation = true
    mapView.delegate = self

    let userLocation = mapView.userLocation
    let region = MKCoordinateRegion.init(center: (userLocation.location?.coordinate)!, latitudinalMeters: 5000, longitudinalMeters: 5000)
    mapView.setRegion(region, animated: true)

}

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.
 }
 */
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    mapView.showsUserLocation = true
    mapView.delegate = self

    let userLocation = mapView.userLocation
    let region = MKCoordinateRegion.init(center: (userLocation.location?.coordinate)!, latitudinalMeters: 5000, longitudinalMeters: 5000)
    mapView.setRegion(region, animated: true)

}

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.
 }
 */
@IBAction func zoomIn(uu发送方:任意){
让userLocation=mapView.userLocation
设region=MKCoordinateRegion.init(中心:(userLocation.location?.coordinate)!,纬度计:5000,纵向计:5000)
self.mapView.setRegion(区域,动画:true)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    mapView.showsUserLocation = true
    mapView.delegate = self

    let userLocation = mapView.userLocation
    let region = MKCoordinateRegion.init(center: (userLocation.location?.coordinate)!, latitudinalMeters: 5000, longitudinalMeters: 5000)
    mapView.setRegion(region, animated: true)

}

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.
 }
 */
}

有一个格式化问题

我不知道为什么会这样,因为以前没有发生过。有人能帮忙吗?任何帮助都将不胜感激

您可以试试这个

if let coor = userLocation.location?.coordinate {
   let region = MKCoordinateRegion(center:coor, latitudinalMeters: 5000, longitudinalMeters: 5000)
}
如果有coor的当前值,它将起作用,您可以在获取位置之前单击它,以便在else中再次请求位置更新,或者最初在
viewdiload

//

使用这种方法

var once = true
//


要获取正确的值,请访问委托的
MKMapViewDelegate
。当发生错误时,控制台中是否有错误消息?您没有获取“userLocation.location?coordinate”的值。@Larme No.只是说(11db)@Khushbu好的,我怎样才能解决这个问题?打印这个用户位置。位置?协调以确认它不是零。这不以用户的当前位置为中心。如果您检查以上内容,我已经更新了它以显示整个脚本您必须有一个当前位置到中心的值,它取决于gps和当前位置,它是异步发生的,您可以在didUpdateLocation方法中使中心化过程仅对委托CLLocationManagerDeLegate执行一次。我不确定我是否完全理解。我是斯威夫特的新手,所以请不要对我太苛刻。你能再解释一下吗?请看编辑,如果你想让它在用户移动时始终居中,请去掉一次。谢谢,修复它的人。所以只是检查一下。如果我想在位置移动时更新整个once变量,我是否要删除它?