CLLocationManagerDelegate 方法不 运行
CLLocationManagerDelegate methods do not run
我正在尝试创建一个只有地图视图的应用程序,上面有一个“显示用户位置”按钮,我在 MapKit
的帮助下实现了该按钮。通过按下按钮,我还想获得用户坐标,为此我使用了 CLLocationManagerDelegate
方法,但我遇到了麻烦 运行。我确实在 plist 中设置了“Privacy - Location When In Use Usage Description”字符串。
这是我的代码 MapViewController
:
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate {
// MARK: - Properties
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var fetchedData = ""
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// MARK: - Actions
// this puts user location on map
@IBAction func showUserLocation() {
// check for location tracing permissions first
let authStatus = CLLocationManager.authorizationStatus()
if authStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
return
} else if authStatus == .denied || authStatus == .restricted {
showLocationServicesDeniedAlert()
return
}
let region = MKCoordinateRegion(center: mapView.userLocation.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
getCoordinates()
}
// MARK: - Helper methods
// handle permission errors
func showLocationServicesDeniedAlert() {
let alert = UIAlertController(title: "Paikannuspalvelut pois päältä", message: "Ole hyvä ja salli paikannuspalvelut tälle äpille.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
// get user coordinates
func getCoordinates() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
// MARK: - MKMapViewDelegate
extension MapViewController: MKMapViewDelegate {
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("*** Location manager failed with error: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
print("*** didUpdateLocations \(newLocation)")
}
当应用程序运行时,请确保位置未设置为“None”。
当模拟器处于活动状态时,您可以在状态栏上找到这些选项。
感谢大家的投入!一旦我移动 CLLocationManagerDelegate
视图控制器内部的方法 class 一切就开始正常工作了。
我了解到您已成功发现 。太好了。
但我可能会建议 adding the protocol conformance in an extension 您的 class,而不是在主要实现中。这使您的代码井井有条。例如:
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController {
// MARK: - Properties
...
// MARK: - Lifecycle
override func viewDidLoad() { ... }
// MARK: - Actions
// this puts user location on map
@IBAction func showUserLocation() { ... }
// MARK: - Helper methods
// handle permission errors
func showLocationServicesDeniedAlert() { ... }
// get user coordinates
func getCoordinates() { ... }
}
// MARK: - MKMapViewDelegate
extension MapViewController: MKMapViewDelegate { ... }
// MARK: - CLLocationManagerDelegate
extension MapViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("*** Location manager failed with error: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
print("*** didUpdateLocations \(newLocation)")
}
}
我正在尝试创建一个只有地图视图的应用程序,上面有一个“显示用户位置”按钮,我在 MapKit
的帮助下实现了该按钮。通过按下按钮,我还想获得用户坐标,为此我使用了 CLLocationManagerDelegate
方法,但我遇到了麻烦 运行。我确实在 plist 中设置了“Privacy - Location When In Use Usage Description”字符串。
这是我的代码 MapViewController
:
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate {
// MARK: - Properties
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var fetchedData = ""
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// MARK: - Actions
// this puts user location on map
@IBAction func showUserLocation() {
// check for location tracing permissions first
let authStatus = CLLocationManager.authorizationStatus()
if authStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
return
} else if authStatus == .denied || authStatus == .restricted {
showLocationServicesDeniedAlert()
return
}
let region = MKCoordinateRegion(center: mapView.userLocation.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
getCoordinates()
}
// MARK: - Helper methods
// handle permission errors
func showLocationServicesDeniedAlert() {
let alert = UIAlertController(title: "Paikannuspalvelut pois päältä", message: "Ole hyvä ja salli paikannuspalvelut tälle äpille.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
// get user coordinates
func getCoordinates() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
// MARK: - MKMapViewDelegate
extension MapViewController: MKMapViewDelegate {
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("*** Location manager failed with error: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
print("*** didUpdateLocations \(newLocation)")
}
当应用程序运行时,请确保位置未设置为“None”。
当模拟器处于活动状态时,您可以在状态栏上找到这些选项。
感谢大家的投入!一旦我移动 CLLocationManagerDelegate
视图控制器内部的方法 class 一切就开始正常工作了。
我了解到您已成功发现
但我可能会建议 adding the protocol conformance in an extension 您的 class,而不是在主要实现中。这使您的代码井井有条。例如:
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController {
// MARK: - Properties
...
// MARK: - Lifecycle
override func viewDidLoad() { ... }
// MARK: - Actions
// this puts user location on map
@IBAction func showUserLocation() { ... }
// MARK: - Helper methods
// handle permission errors
func showLocationServicesDeniedAlert() { ... }
// get user coordinates
func getCoordinates() { ... }
}
// MARK: - MKMapViewDelegate
extension MapViewController: MKMapViewDelegate { ... }
// MARK: - CLLocationManagerDelegate
extension MapViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("*** Location manager failed with error: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
print("*** didUpdateLocations \(newLocation)")
}
}