如何将坐标从 didUpdateLocation 传送到某个视图控制器?
how to convey the coordinate from didUpdateLocation to some view controller?
我正在尝试制作 LocationService
class 来传达 userCoordinate,因此它可以被多个 View Controller 重复使用
import UIKit
import CoreLocation
class LocationService: NSObject {
let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
func getPermission() {
// to ask permission to the user by showing an alert (the alert message is available on info.plist)
if CLLocationManager.authorizationStatus() == .notDetermined {
manager.requestWhenInUseAuthorization()
}
}
func checkLocationAuthorizationStatus() -> CLAuthorizationStatus{
return CLLocationManager.authorizationStatus()
}
}
extension LocationService : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
manager.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("location is not available: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let userLocation = locations.first else {return}
if userLocation.horizontalAccuracy > 0 {
manager.stopUpdatingLocation()
let coordinate = Coordinate(location: userLocation)
// how to convey this coordinate for several view controller?
}
}
}
正如您在来自 CLLocationManagerDelegate
的 didUpdateLocations
方法中看到的那样,坐标需要一些时间才能生成。
但我不知道如何传达该用户坐标,我认为它将使用完成处理程序,但我不知道如何获取它
所以假设在 HomeVC 中,我将调用 LocationService
来获取用户坐标
import UIKit
class HomeVC: UIViewController {
let locationService = LocationService()
override func viewDidLoad() {
super.viewDidLoad()
// get coordinate, something like this
locationService.getCoordinate()
}
}
你可以像 Paulw11 说的那样使用 Notification
。您需要更新 didUpdateLocations
。这是您要去 post 通知的地方。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let userLocation = locations.first else {return}
if userLocation.horizontalAccuracy > 0 {
manager.stopUpdatingLocation()
let coordinate = Coordinate(location: userLocation)
let locationDictionary: [String: Double] = ["lat": location.coordinate.latitude,
"long": location.coordinate.longitude]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "YourNotificationNameAsYouWantToNameIt"), object: nil, userInfo: locationDictionary)
}
}
现在 viewDidLoad
在每个你想要这个位置的视图控制器中你需要观察这个通知:
NotificationCenter.default.addObserver(self, selector: #selector(doSomethingAboutLocation(_:)), name: NSNotification.Name(rawValue: "YourNotificationNameAsYouWantToNameIt"), object: nil)
然后在从选择器调用的函数中像这样访问您的位置:
@objc func doSomethingAboutLocation(_ notification: Notification) {
if let notificationInfo = notification.userInfo {
let coordinate = CLLocation(latitude: notificationInfo["lat"] as! Double, longitude: notificationInfo["long"] as! Double)
// use your coordinate as you want
}
}
我正在尝试制作 LocationService
class 来传达 userCoordinate,因此它可以被多个 View Controller 重复使用
import UIKit
import CoreLocation
class LocationService: NSObject {
let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
func getPermission() {
// to ask permission to the user by showing an alert (the alert message is available on info.plist)
if CLLocationManager.authorizationStatus() == .notDetermined {
manager.requestWhenInUseAuthorization()
}
}
func checkLocationAuthorizationStatus() -> CLAuthorizationStatus{
return CLLocationManager.authorizationStatus()
}
}
extension LocationService : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
manager.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("location is not available: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let userLocation = locations.first else {return}
if userLocation.horizontalAccuracy > 0 {
manager.stopUpdatingLocation()
let coordinate = Coordinate(location: userLocation)
// how to convey this coordinate for several view controller?
}
}
}
正如您在来自 CLLocationManagerDelegate
的 didUpdateLocations
方法中看到的那样,坐标需要一些时间才能生成。
但我不知道如何传达该用户坐标,我认为它将使用完成处理程序,但我不知道如何获取它
所以假设在 HomeVC 中,我将调用 LocationService
来获取用户坐标
import UIKit
class HomeVC: UIViewController {
let locationService = LocationService()
override func viewDidLoad() {
super.viewDidLoad()
// get coordinate, something like this
locationService.getCoordinate()
}
}
你可以像 Paulw11 说的那样使用 Notification
。您需要更新 didUpdateLocations
。这是您要去 post 通知的地方。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let userLocation = locations.first else {return}
if userLocation.horizontalAccuracy > 0 {
manager.stopUpdatingLocation()
let coordinate = Coordinate(location: userLocation)
let locationDictionary: [String: Double] = ["lat": location.coordinate.latitude,
"long": location.coordinate.longitude]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "YourNotificationNameAsYouWantToNameIt"), object: nil, userInfo: locationDictionary)
}
}
现在 viewDidLoad
在每个你想要这个位置的视图控制器中你需要观察这个通知:
NotificationCenter.default.addObserver(self, selector: #selector(doSomethingAboutLocation(_:)), name: NSNotification.Name(rawValue: "YourNotificationNameAsYouWantToNameIt"), object: nil)
然后在从选择器调用的函数中像这样访问您的位置:
@objc func doSomethingAboutLocation(_ notification: Notification) {
if let notificationInfo = notification.userInfo {
let coordinate = CLLocation(latitude: notificationInfo["lat"] as! Double, longitude: notificationInfo["long"] as! Double)
// use your coordinate as you want
}
}