如何在多个 ViewController 中使用 locationManager()

Posted

技术标签:

【中文标题】如何在多个 ViewController 中使用 locationManager()【英文标题】:How to use locationManager() in multiple ViewControllers 【发布时间】:2018-12-15 21:25:02 【问题描述】:

我需要在多个视图控制器中获取zipCodecity

这是我目前的做法......

import CoreLocation
let locationManager = CLLocationManager()

class MyViewController: UIViewController, CLLocationManagerDelegate
    override func viewDidLoad() 
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
    


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: (placemarks, error)-> Void in
            if error != nil 
                //AlertView to show the ERROR message
            

            if placemarks!.count > 0 
                let placemark = placemarks![0]
                self.locationManager.stopUpdatingLocation()
                let zipCode = placemark.postalCode ?? ""
                let city:String = placemark.locality ?? ""

                // Do something with zipCode
                // Do something with city
            else
                print("No placemarks found.")
            
        )
    

 func someFunction() 
    locationManager.startUpdatingLocation()
 

一切正常,但正如您所见,在多个 viewController 中这样做会导致大量代码重复(当然,我没有展示整个代码)。

从多个视图控制器中以更实用的方式从CLLocationManager() 中检索zipCodecity 的最常见方法是什么?

我的想法是……

 MyLocationManager.zipCode() // returns zipCode as a string 
 MyLocationManager.city() // returns city as a string 

【问题讨论】:

如果你创建一个单例来跟踪用户位置呢?检查以下内容,它可能会有所帮助:***.com/questions/11513259/… 【参考方案1】:

通常的做法是在一个持久的地方只拥有一个位置管理器,您可以随时从任何地方访问,例如应用程序委托或根视图控制器。

【讨论】:

对不起,我有点困惑,您介意在您的声明The usual thing is to have just one location manager in one persistent place 中详细说明一下吗?再次抱歉,但我有点困惑。【参考方案2】:

我尝试实现一个单例CLLocationManager类,我想你可以修改下面的类来实现一些额外的方法。

import Foundation

class LocationSingleton: NSObject, CLLocationManagerDelegate 
private let locationManager = CLLocationManager()
private var latitude = 0.0
private var longitude = 0.0

static let shared = LocationSingleton()

private override init() 
    super.init()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLLocationAccuracyHundredMeters
    locationManager.requestAlwaysAuthorization() // you might replace this with whenInuse
    locationManager.startUpdatingLocation()


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    if let location = locations.last 
        latitude = location.coordinate.latitude
        longitude = location.coordinate.longitude
    


private func getLatitude() -> CLLocationDegrees 
    return latitude


private func getLongitude() -> CLLocationDegrees 
    return longitude


private func zipCode() 
    // I think you can figure way out to implemet this method


private func city() 
    // I think you can figure way out to implemet this method


【讨论】:

非常感谢您的帮助,快速提问,您如何在每次从其他视图控制器调用 LocationSingleton.shared.getLatitude()getLongitude() 方法时更新位置?您在调用getLatitude()getLongitude() 方法之前调用的公共函数中是否有locationManager.startUpdatingLocation(),或者是从getLatitude()getLatitude() 内部调用locationManager.startUpdatingLocation() getLongitude() 方法。我很想知道你是怎么做到的。 @-emrepun 我上面问题的主要原因是因为我第一次打电话给LocationSingleton.shared.getLatitude() 直到第二次打电话我才得到任何东西。 也许当你第一次调用该方法时,位置根本没有更新,你可以尝试在手机上运行它,走动一下,然后用一些 asyncAfter 方法调用 LocationSingleton。 shared.getLatitude() 方法,看看它是否仍然没有为第一次调用提供任何东西。 @fs_tigre 我认为问题在于代码运行的顺序。例如,如果我从我的一个 viewControllers 调用 LocationSingleton.shared.getLatitude() 并记录序列,这就是我所看到的......首先调用的当然是 init 方法,然后第二个是 getLatitude 和最后是locationManager,这就是它第一次没有更新的原因。 是的,我明白了,我会尽力想出一个解决方案并在这里告诉你,如果你能找到一个简单的方法来解决这个问题,请在这里评论@fs_tigre

以上是关于如何在多个 ViewController 中使用 locationManager()的主要内容,如果未能解决你的问题,请参考以下文章