Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. //
  2. // LocationService.swift
  3. //
  4. //
  5. // Created by Anak Mirasing on 5/18/2558 BE.
  6. //
  7. //
  8.  
  9. import Foundation
  10. import CoreLocation
  11.  
  12. protocol LocationServiceDelegate {
  13. func tracingLocation(currentLocation: CLLocation)
  14. func tracingLocationDidFailWithError(error: NSError)
  15. }
  16.  
  17. class LocationService: NSObject, CLLocationManagerDelegate {
  18.  
  19. // class var sharedInstance: LocationService {
  20. // struct Static {
  21. // static var onceToken: dispatch_once_t = 0
  22.  
  23. // static var instance: LocationService? = nil
  24. // }
  25. // dispatch_once(&Static.onceToken) {
  26. // Static.instance = LocationService()
  27. // }
  28. // return Static.instance!
  29. // }
  30.  
  31. static let sharedInstance = LocationService() //Simple, Short & Sweet :)
  32.  
  33. var locationManager: CLLocationManager?
  34. var lastLocation: CLLocation?
  35. var delegate: LocationServiceDelegate?
  36.  
  37. override init() {
  38. super.init()
  39.  
  40. self.locationManager = CLLocationManager()
  41. guard let locationManager = self.locationManager else {
  42. return
  43. }
  44.  
  45. if CLLocationManager.authorizationStatus() == .NotDetermined {
  46. // you have 2 choice
  47. // 1. requestAlwaysAuthorization
  48. // 2. requestWhenInUseAuthorization
  49. locationManager.requestAlwaysAuthorization()
  50. }
  51.  
  52. locationManager.desiredAccuracy = kCLLocationAccuracyBest // The accuracy of the location data
  53. locationManager.distanceFilter = 200 // The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
  54. locationManager.delegate = self
  55. }
  56.  
  57. func startUpdatingLocation() {
  58. print("Starting Location Updates")
  59. self.locationManager?.startUpdatingLocation()
  60. }
  61.  
  62. func stopUpdatingLocation() {
  63. print("Stop Location Updates")
  64. self.locationManager?.stopUpdatingLocation()
  65. }
  66.  
  67. // CLLocationManagerDelegate
  68. func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  69.  
  70. guard let location = locations.last else {
  71. return
  72. }
  73.  
  74. // singleton for get last location
  75. self.lastLocation = location
  76.  
  77. // use for real time update location
  78. updateLocation(location)
  79. }
  80.  
  81. func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
  82.  
  83. // do on error
  84. updateLocationDidFailWithError(error)
  85. }
  86.  
  87. // Private function
  88. private func updateLocation(currentLocation: CLLocation){
  89.  
  90. guard let delegate = self.delegate else {
  91. return
  92. }
  93.  
  94. delegate.tracingLocation(currentLocation)
  95. }
  96.  
  97. private func updateLocationDidFailWithError(error: NSError) {
  98.  
  99. guard let delegate = self.delegate else {
  100. return
  101. }
  102.  
  103. delegate.tracingLocationDidFailWithError(error)
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement