[英]Custom CLLocationManager didUpdateLocations Function
I read the Radar.io
( https://radar.io/documentation/sdk#ios ) iOS SDK documentation and I'm curious how I create a custom 我阅读了Radar.io
( https://radar.io/documentation/sdk#ios )iOS SDK文档,并且很好奇如何创建自定义
locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
function. locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
函数。
class ViewController: UIViewController, RadarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
Radar.setDelegate(self)
}
}
And 和
Radar.trackOnce(completionHandler: { (status: RadarStatus, location: CLLocation?, events: [RadarEvent]?, user: RadarUser?) in
// do something with status, location, events, user
})
source: https://radar.io/documentation/sdk#ios 来源: https : //radar.io/documentation/sdk#ios
How i get location update and control CLLocationManager in a custom class like this? 我如何在这样的自定义类中获取位置更新和控制CLLocationManager?
protocol MyRadarCloneDelegate {
}
class MyRadarClone {
static func setDelegate(_ delegate:) {
}
static func startTracking() {
}
// some other function for control CLLocationManager
}
If I understand your question correctly you want to access your location
from Radar.trackOnce
into your custom class. 如果我正确理解你的问题,你要访问您的location
从Radar.trackOnce
到您的自定义类。
You can do it this way. 您可以这样进行。
Update your startTracking
method this way: 通过以下方式更新您的startTracking
方法:
func startTracking(location: CLLocation) {
//do somtrhing with your location
print(location)
}
And into your Radar.trackOnce
method add below code: 并在Radar.trackOnce
方法中添加以下代码:
guard let userLocation = location else {return}
let myRadar = MyRadarClone()
myRadar.startTracking(location: userLocation)
Hope this will help. 希望这会有所帮助。
Note: Code is not tested. 注意:代码未经测试。
Don't create any custom protocols or functions for this 不要为此创建任何自定义协议或功能
I think this is a best idea 我认为这是个好主意
I'm assuming the objective is to create a custom lifecycle method that acts like viewDidLayoutSubviews
but is instead locationManagerDidGetLocation
, or something, that is called only once in any view controller you put it in. If instead you simply want to extend the location manager's delegate to other view controllers, the strategy is the same (with a small adjustment at the end): 我假设目标是创建一个自定义生命周期方法,该方法的行为类似于viewDidLayoutSubviews
但它是locationManagerDidGetLocation
或类似的东西,在您放入的任何视图控制器中仅调用一次。如果相反,您只是想扩展位置管理器的委托对于其他视图控制器,策略是相同的(最后稍作调整):
class SomeViewController: UIViewController {
func locationManagerDidGetLocation() {
doSomethingThatRequiresUserLocation()
}
}
CLLocationManagerDelegate
has a method for new location data (and one for new location value). CLLocationManagerDelegate
有一种用于新位置数据的方法(以及一种用于新位置值的方法)。 Simply post a notification from it: 只需从中发布通知:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
NotificationCenter.default.post(name: .locationManagerDidUpdateLocations, object: nil)
}
You can also share the value of the location across your app in a static structure variable or in the app's delegate: 您还可以在静态结构变量或应用程序的委托中共享整个应用程序中位置的值:
struct CurrentLocation {
static var coordinate: CLLocationCoordinate2D?
}
And update it through the delegate: 并通过委托更新它:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// update shared property
let lastLocation = locations.last!
CurrentLocation.coordinate = lastLocation.coordinate
// post app-wide notification
NotificationCenter.default.post(name: .locationManagerDidUpdateLocations, object: nil)
}
To access that value just call CurrentLocation.coordinate
anywhere in your app. 要访问该值,只需在应用程序中的任何位置调用CurrentLocation.coordinate
。
Then simply add a notification observer to an object, like a UIViewController
, listening for that post: 然后只需向对象(如UIViewController
添加一个通知观察者,以侦听该帖子:
class SomeViewController: UIViewController {
func addNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(locationManagerDidUpdateLocationsHandler), name: .locationManagerDidUpdateLocations, object: nil)
}
}
And when the first post arrives, remove the observer, so it only gets called once, and call your custom method: 当第一条帖子到达时,删除观察者,这样它只会被调用一次,并调用您的自定义方法:
@objc func locationManagerDidUpdateLocationsHandler() {
NotificationCenter.default.removeObserver(self, name: .locationManagerDidUpdateLocations, object: nil)
locationManagerDidGetLocation()
}
And now it works like a regular lifecycle method that is fired only once when the user's location is available: 现在,它就像常规的生命周期方法一样工作,当用户的位置可用时仅触发一次:
class SomeViewController: UIViewController {
func locationManagerDidGetLocation() {
doSomethingThatRequiresUserLocation()
}
}
You can, of course, not remove the observer and have it function like an extended delegate of the location manager. 当然,您不能删除观察者,并使它的作用类似于位置管理器的扩展委托。 And don't forget to remove all observers in deinit
if these view controllers don't persist the life of the app. 而且,如果这些视图控制器不能保持应用程序的生命周期,请不要忘记删除deinit
中的所有观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.