| | |
| | | return vc |
| | | }() |
| | | |
| | | private lazy var manager:CLLocationManager = { |
| | | let mana = CLLocationManager() |
| | | mana.delegate = self |
| | | mana.requestAlwaysAuthorization() |
| | | mana.distanceFilter = 20 |
| | | mana.desiredAccuracy = 10 |
| | | return mana |
| | | private lazy var mananger:CLLocationManager = { |
| | | let manan = CLLocationManager() |
| | | manan.delegate = self |
| | | manan.requestWhenInUseAuthorization() |
| | | manan.requestAlwaysAuthorization() |
| | | manan.distanceFilter = 10 |
| | | manan.allowsBackgroundLocationUpdates = true |
| | | manan.pausesLocationUpdatesAutomatically = false |
| | | manan.desiredAccuracy = kCLLocationAccuracyBestForNavigation |
| | | return manan |
| | | }() |
| | | |
| | | override func viewDidLoad() { |
| | | super.viewDidLoad() |
| | | |
| | | manager.startUpdatingLocation() |
| | | |
| | | uploadLocation() |
| | | } |
| | | |
| | | private func uploadLocation(){ |
| | | if let str = UserDefaults.standard.object(forKey: "CurrentOrder") as? String,let model = OrderDetailModel.deserialize(from: str){ |
| | | switch model.data?.status{ |
| | | case .PendingPickupDeparture,.ArrivedPort,.SendTOCheckPoint,.Checking,.PendingPickupInCheckPoint,.InTransitInYard,.InYard,.Transiting,.InWarehouse,.PendingUnload,.Unloaded,.TransitingToYard,.InYardByEmpty,.BackYard: |
| | | mananger.startUpdatingLocation() |
| | | default: |
| | | mananger.stopUpdatingLocation() |
| | | } |
| | | } |
| | | } |
| | | |
| | | override func setUI() { |
| | | super.setUI() |
| | |
| | | |
| | | func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { |
| | | if let location = locations.last{ |
| | | Services.uploadLocation(coordinate: location.coordinate).subscribe(onNext: {data in |
| | | var type:Int = 2 |
| | | if let previousLocation = UserDefaults.standard.object(forKey: "PreviousLocation") as? String{ |
| | | let lat = previousLocation.components(separatedBy: ",").first?.toDouble ?? 0 |
| | | let lon = previousLocation.components(separatedBy: ",").last?.toDouble ?? 0 |
| | | let distance = location.distance(from: CLLocation(latitude: lat, longitude: lon)) |
| | | |
| | | if var dis = UserDefaults.standard.object(forKey: "filterDistance") as? Double{ |
| | | dis += distance |
| | | if dis >= 3000{ |
| | | dis = 0;type = 1 |
| | | } |
| | | UserDefaults.standard.set(dis, forKey: "filterDistance") |
| | | UserDefaults.standard.synchronize() |
| | | }else{ |
| | | UserDefaults.standard.set(distance, forKey: "filterDistance") |
| | | UserDefaults.standard.synchronize() |
| | | } |
| | | }else{ |
| | | type = 1 |
| | | } |
| | | print("---->\(type)") |
| | | Services.uploadLocation(coordinate: location.coordinate,type: type).subscribe(onNext: {data in |
| | | UserDefaults.standard.set("\(location.coordinate.latitude),\(location.coordinate.longitude)", forKey: "PreviousLocation") |
| | | UserDefaults.standard.synchronize() |
| | | }).disposed(by: disposeBag) |
| | | } |
| | | } |