线程1:EXC_BREAKPOINT(代码= 1,子代码= ...)使用MKMap视图时

时间:2018-08-21 12:25:15

标签: ios swift xcode

所以我最近遇到了我的MKMapView问题。每当我单击一个按钮进入带有地图的视图时,都会出现上述错误,且以该线为中心。

已使用完整脚本更新。错误是let region = MKCoordinateRegion.init(center: (userLocation.location?.coordinate)!, latitudinalMeters: 5000, longitudinalMeters: 5000) mapView.setRegion(region, animated: true) }

@IBOutlet weak var mapView: MKMapView!
@IBAction func mapTypeChanged(_ sender: Any) {
    switch ((sender as AnyObject).selectedSegmentIndex) {
    case 0:
        mapView.mapType = .standard
    case 1:
        mapView.mapType = .hybridFlyover
    default:
        mapView.mapType = .standard
}

}

@IBAction func zoomIn(_ sender: Any) { let userLocation = mapView.userLocation let region = MKCoordinateRegion.init(center: (userLocation.location?.coordinate)!, latitudinalMeters: 5000, longitudinalMeters: 5000) self.mapView.setRegion(region, animated: 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.
 }
 */

}

存在格式问题。

我不知道为什么这是以前没有发生过的。有人可以帮忙吗?任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您可以尝试

if let coor = userLocation.location?.coordinate {
   let region = MKCoordinateRegion(center:coor, latitudinalMeters: 5000, longitudinalMeters: 5000)
}

如果存在coor的当前值,它将起作用,也许您在获取位置之前单击它,以便再次请求在else或最初在viewDidLoad内的位置更新

//

使用此方法

var once = true

//

func mapView(_ mapView: MKMapView, 
        didUpdate userLocation: MKUserLocation) {
     if once {
      let region = MKCoordinateRegion.init(center: userLocation.location!.coordinate, latitudinalMeters: 5000, longitudinalMeters: 5000)
      mapView.setRegion(region, animated: true)
      once = false
     }
}

委托人MKMapViewDelegate以获取正确的值