【发布时间】:2016-04-13 12:42:04
【问题描述】:
我目前正在处理一个需要我在应用中获取用户位置的项目。最初我将它编码为仅显示一个工作地图视图,但后来想让它显示用户位置。我一直收到错误 EXC_BAD_INSTRUCTION 在线
self.mapView.showsUserLocation = true
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
{
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Location Delegate Methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.latitude)
let region = MKCoordinateRegion(center: center, span:MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
self.locationManager.startUpdatingLocation()
}
【问题讨论】:
-
最可能的原因:self.mapView 为 nil,因为它没有连接到 Interface Builder 中的 Map View
标签: xcode swift mapkit cllocationmanager