我正在使用 swift 版本 5.7.1 和 Xcode 14.1 。我正在創建具有用戶位置的地圖應用程式并且它作業正常..但這是問題所在,它發出警告..
如果在主執行緒上呼叫此方法可能會導致 UI 無回應。相反,請考慮等待-locationManagerDidChangeAuthorization:回呼并authorizationStatus先檢查。.
在這條線上..if CLLocationManager.locationServicesEnabled() {
我已經添加到主執行緒中。但仍然是同樣的警告..這是代碼..
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
let mapView = MKMapView()
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(mapView)
mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
DispatchQueue.main.async {
self.manager.delegate = self
self.manager.desiredAccuracy = kCLLocationAccuracyBest
self.manager.startUpdatingLocation()
self.mapView.delegate = self
self.mapView.mapType = .standard
self.mapView.isZoomEnabled = true
self.mapView.isScrollEnabled = true
self.mapView.showsUserLocation = false
}
}
if let coor = mapView.userLocation.location?.coordinate{
mapView.setCenter(coor, animated: true)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
guard let mylocation = manager.location else { return }
let myCoordinates: CLLocationCoordinate2D = mylocation.coordinate
mapView.mapType = MKMapType.standard
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegion(center: myCoordinates, span: span)
mapView.setRegion(region, animated: true)
// comment pin object if showsUserLocation = true
let pin = MKPointAnnotation()
pin.coordinate = myCoordinates
pin.title = "You are here"
mapView.addAnnotation(pin)
}
}
這是螢屏截圖..

uj5u.com熱心網友回復:
警告告訴您根本不應該呼叫CLLocationManager.locationServicesEnabled(),因為它可能需要很長時間并導致 UI 打嗝。相反,您應該實作委托功能locationManagerDidChangeAuthorization(_:),檢查authorizationStatus 并將結果存盤在某處,然后在您的if.
uj5u.com熱心網友回復:
嘗試使用 locationManagerDidChangeAuthorization:
class Prova: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
let mapView = MKMapView()
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(mapView)
mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
locationAuthorization()
}
func locationAuthorization(){
switch manager.authorizationStatus {
case .authorizedWhenInUse:
print("Authorized")
determineMyCurrentLocation()
case .denied:
break
case .authorizedAlways:
determineMyCurrentLocation()
case.notDetermined:
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
sleep(2)
locationAuthorization()
default:
break
}
}
func determineMyCurrentLocation() {
print("determine current location")
if CLLocationManager.locationServicesEnabled() {
DispatchQueue.main.async {
self.manager.delegate = self
self.manager.desiredAccuracy = kCLLocationAccuracyBest
self.manager.startUpdatingLocation()
self.mapView.delegate = self
self.mapView.mapType = .standard
self.mapView.isZoomEnabled = true
self.mapView.isScrollEnabled = true
self.mapView.showsUserLocation = false // if you want to show default pin
}
}
if let coor = mapView.userLocation.location?.coordinate {
mapView.setCenter(coor, animated: true)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
guard let mylocation = manager.location else { return }
let myCoordinates: CLLocationCoordinate2D = mylocation.coordinate
mapView.mapType = MKMapType.standard
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegion(center: myCoordinates, span: span)
mapView.setRegion(region, animated: true)
let pin = MKPointAnnotation()
pin.coordinate = myCoordinates
pin.title = "You are here"
mapView.addAnnotation(pin)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/534958.html
標籤:迅速地图套件场地经理
上一篇:如何快速將小時轉換為秒?[復制]
