gpt4 book ai didi

ios - 如何将坐标从 didUpdateLocation 传送到某个 View Controller ?

转载 作者:行者123 更新时间:2023-11-28 14:06:48 24 4
gpt4 key购买 nike

我正在尝试制作将传送 userCoordinate 的 LocationService 类,因此它可以被多个 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?
}
}
}

正如您在 CLLocationManagerDelegatedidUpdateLocations 方法中看到的那样,坐标需要一些时间才能生成。

但我不知道如何传达该用户坐标,我认为它将使用完成处理程序但我不知道如何获取它

假设在 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。这是您要发布通知的地方。

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 中,在每个你想要这个位置的 View Controller 中,你需要观察这个通知:

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
}
}

关于ios - 如何将坐标从 didUpdateLocation 传送到某个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52923277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com