Xcode表示存在线程1:信号SIGABRT。它还说libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)。我是初学者,因此请“轻松”回复;)
// ViewController.swift
import UIKit
import MapKit
import CoreLocation
class MapScreen: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
checkLocationServices()
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuthorization()
}else{
// show alert letting the user know he has to turn them on.
}
}
func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
break
case .denied:
// show alert instructing how to turn on permissions
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
// show an alert letting them know what's up
break
case .authorizedAlways:
break
}
}
}
extension MapScreen: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// later
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
// later
}
}
答案 0 :(得分:0)
要填充圆圈(将UI控件连接到情节提要的@IBOutlet
旁边),您需要同时打开.swift
文件和情节提要,并尝试确保它们确实已连接>
MapScreen.swift
文件,如果连接器已连接,则应填充连接器Assistant Editor
,以便同时打开情节提要和MapScreen.swift
文件,并确保在MapScreen
中将Class设置为Identity Inspector
,例如下面的屏幕截图我还将列出一些有关MKMapView和LocationManager的建议
locationManager.requestWhenInUseAuthorization()
没有以下使用说明中的一项或多项,则 Info.plist
将不起作用
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Message for AlwaysAndWhenInUseUsageDescription</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Message for AlwaysUsageDescription</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Message for WhenInUseUsageDescription</string>
一旦获得使用位置服务的权限,就必须询问locationManager.startUpdatingLocation()
,这样您才能通过CLLocationManagerDelegate
locationManager(_:didUpdateLocations:)
中接收的,为了能够在地图上查看用户位置,我们需要使用mapView.setRegion
将地图区域设置为该位置这是更新的课程
class MapScreen: UIViewController {
@IBOutlet var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
setupLocationManager()
}
func setupLocationManager() {
guard CLLocationManager.locationServicesEnabled() else {
// show alert letting the user know he has to turn them on.
print("Location Servies: Disabled")
return
}
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
checkLocationAuthorization()
}
func checkLocationAuthorization(authorizationStatus: CLAuthorizationStatus? = nil) {
switch (authorizationStatus ?? CLLocationManager.authorizationStatus()) {
case .authorizedAlways, .authorizedWhenInUse:
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
case .restricted, .denied:
// show alert instructing how to turn on permissions
print("Location Servies: Denied / Restricted")
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
}
}
}
extension MapScreen: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.count > 1 ? locations.sorted(by: { $0.timestamp < $1.timestamp }).last : locations.first else { return }
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: location.coordinate, span: span)
mapView.setRegion(region, animated: true)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
self.checkLocationAuthorization(authorizationStatus: status)
}
}
产生