代码之家  ›  专栏  ›  技术社区  ›  fs_tigre

如何在多个视图控制器中使用locationManager()。

  •  -1
  • fs_tigre  · 技术社区  · 6 年前

    我需要得到 zipCode 以及 city 在多个视图控制器中。

    以下是我目前的做法…

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

    一切都很好,但是正如您在多视图控制器中看到的那样,这样做会导致大量代码重复(当然,我并没有显示整个代码)。

    最常见的检索方法是什么? 邮政编码 城市 CLLocationManager() 以更实际的方式从多个视图控制器?

    我想的是…

     MyLocationManager.zipCode() // returns zipCode as a string 
     MyLocationManager.city() // returns city as a string 
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   emrepun    6 年前

    我试图实现单件 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
    }
    }
    
        2
  •  2
  •   matt    6 年前

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