创建Corelocation模型类

时间:2016-08-05 14:14:57

标签: ios swift cllocation

我在几个视图中使用Corelocation,我使用以下代码:

import UIKit
import CoreLocation

class ModelCL: UIViewController, CLLocationManagerDelegate {

let LocationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    //CoreLocation
    self.LocationManager.delegate = self
    self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.LocationManager.requestWhenInUseAuthorization()
    self.LocationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
        (placemarks, error) -> Void in

        if error != nil {
            print("Error")
        }

        if let pm = placemarks?.first {
            self.displayLocationInfo(pm)
        }
        else {
            print("Error : data error")
        }

    })
}

func displayLocationInfo (placemark: CLPlacemark) {

    self.LocationManager.stopUpdatingLocation()

    print(placemark.subThoroughfare)
    print(placemark.thoroughfare)
    print(placemark.locality)
    print(placemark.postalCode)
    print(placemark.subAdministrativeArea)
    print(placemark.administrativeArea)
    print(placemark.country)

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error :" + error.localizedDescription)
}
}

它在每个视图上运行良好,因为我在每个视图上都实现了它。使用此代码只创建一个Model Class并调用每个视图所需的管理级别不是更好吗?我无法弄清楚如何在另一个视图上调用它...

0 个答案:

没有答案