Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.53 KB | None | 0 0
  1. import MapKit
  2.  
  3. protocol LocationUpdateProtocol {
  4.     func locationDidUpdateToLocation(location : CLLocation)
  5. }
  6.  
  7. /// Notification on update of location. UserInfo contains CLLocation for key "location"
  8. let kLocationDidChangeNotification = "LocationDidChangeNotification"
  9.  
  10. class UserLocationManager: NSObject, CLLocationManagerDelegate {
  11.    
  12.     static let SharedManager = UserLocationManager()
  13.    
  14.     private var locationManager = CLLocationManager()
  15.    
  16.     var currentLocation : CLLocation?
  17.    
  18.     var delegate : LocationUpdateProtocol!
  19.    
  20.     private override init () {
  21.         super.init()
  22.         self.locationManager.delegate = self
  23.         self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
  24.         self.locationManager.distanceFilter = kCLLocationAccuracyHundredMeters
  25.         locationManager.requestAlwaysAuthorization()
  26.         self.locationManager.startUpdatingLocation()
  27.     }
  28.    
  29.     // MARK: - CLLocationManagerDelegate
  30.    
  31.     func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
  32.         currentLocation = newLocation
  33.         let userInfo : NSDictionary = ["location" : currentLocation!]
  34.        
  35.         DispatchQueue.main.async { () -> Void in
  36.             self.delegate.locationDidUpdateToLocation(self.currentLocation!)
  37.             NSNotificationCenter.defaultCenter().postNotificationName(kLocationDidChangeNotification, object: self, userInfo: userInfo as [NSObject : AnyObject])
  38.         }
  39.     }
  40.    
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement