|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
import UIKit
|
|
|
import CoreLocation
|
|
|
+import CoreData
|
|
|
|
|
|
class ViewController: UIViewController {
|
|
|
|
|
@@ -21,11 +22,14 @@ class ViewController: UIViewController {
|
|
|
|
|
|
var noOfSentPos: Int = 0
|
|
|
|
|
|
+ var gpsLogData: [NSManagedObject] = []
|
|
|
+
|
|
|
+ let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
+
|
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
|
- clickButton.isHidden = false
|
|
|
locationManager = CLLocationManager()
|
|
|
locationManager?.delegate = self
|
|
|
locationManager?.requestWhenInUseAuthorization()
|
|
@@ -35,14 +39,6 @@ class ViewController: UIViewController {
|
|
|
locationManager?.distanceFilter = 100.0
|
|
|
locationManager?.allowsBackgroundLocationUpdates = true
|
|
|
pauseSwitch.isEnabled = false
|
|
|
-
|
|
|
- //noOfReceivedGPSPos.text = "KALLE"
|
|
|
- }
|
|
|
-
|
|
|
- @IBAction func buttonClicked(_ sender: Any) {
|
|
|
- print("Clicked !")
|
|
|
- //locationManager?.requestLocation()
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@IBAction func tackingPauseChanged(_ sender: UISwitch) {
|
|
@@ -50,8 +46,10 @@ class ViewController: UIViewController {
|
|
|
switch sender.isOn {
|
|
|
case true:
|
|
|
print("Pause ON")
|
|
|
+ locationManager?.stopUpdatingLocation()
|
|
|
default:
|
|
|
print("Pause OFF")
|
|
|
+ locationManager?.startUpdatingLocation()
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -59,9 +57,20 @@ class ViewController: UIViewController {
|
|
|
print("Switch Changed !")
|
|
|
switch sender.isOn {
|
|
|
case true:
|
|
|
+ let managedContext = appDelegate.persistentContainer.viewContext
|
|
|
+ //let entity = NSEntityDescription.entity(forEntityName: "GPS", in: managedContext)!
|
|
|
+ //let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "GPS")
|
|
|
+ let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "GPS")
|
|
|
+ let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
|
|
|
+ do {
|
|
|
+ try managedContext.execute(batchDeleteRequest)
|
|
|
+ } catch {
|
|
|
+ // Error Handling
|
|
|
+ }
|
|
|
print("ON")
|
|
|
locationManager?.startUpdatingLocation()
|
|
|
pauseSwitch.isEnabled = true
|
|
|
+ noOfSentPos = 0
|
|
|
default:
|
|
|
print("OFF")
|
|
|
locationManager?.stopUpdatingLocation()
|
|
@@ -104,21 +113,44 @@ extension ViewController: CLLocationManagerDelegate {
|
|
|
|
|
|
// Step 7: Handle the location information
|
|
|
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
|
- print("LocationManager didUpdateLocations: numberOfLocation: \(locations.count)")
|
|
|
- let dateFormatter = DateFormatter()
|
|
|
- dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
|
|
|
|
|
+ print("LocationManager didUpdateLocations: numberOfLocation: \(locations.count)")
|
|
|
+ let dateFormatter = DateFormatter()
|
|
|
+ dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
|
|
|
|
|
noOfSentPos += 1
|
|
|
noOfRxGPSPos.text = String(noOfSentPos)
|
|
|
|
|
|
- placeOrder(didUpdateLocations: locations)
|
|
|
+ //placeOrder(didUpdateLocations: locations)
|
|
|
+
|
|
|
+ let managedContext = appDelegate.persistentContainer.viewContext
|
|
|
+ let entity = NSEntityDescription.entity(forEntityName: "GPS", in: managedContext)!
|
|
|
+
|
|
|
|
|
|
- locations.forEach { (location) in
|
|
|
- print("LocationManager didUpdateLocations: \(dateFormatter.string(from: location.timestamp)); \(location.coordinate.latitude), \(location.coordinate.longitude)")
|
|
|
- print("LocationManager horizontalAccuracy: \(location.horizontalAccuracy)")
|
|
|
- print("LocationManager verticalAccuracy: \(location.verticalAccuracy)")
|
|
|
- }
|
|
|
+ locations.forEach { (location) in
|
|
|
+ //print("LocationManager didUpdateLocations: \(dateFormatter.string(from: location.timestamp)); \(location.coordinate.latitude), \(location.coordinate.longitude)")
|
|
|
+ print("LocationManager horizontalAccuracy: \(location.horizontalAccuracy)")
|
|
|
+ //print("LocationManager verticalAccuracy: \(location.verticalAccuracy)")
|
|
|
+
|
|
|
+ let pos = NSManagedObject(entity: entity,insertInto: managedContext)
|
|
|
+ pos.setValue(location.coordinate.longitude, forKey: "longitud")
|
|
|
+ pos.setValue(location.coordinate.latitude, forKey: "latitude")
|
|
|
+ pos.setValue(location.timestamp, forKey: "ts")
|
|
|
+ do {
|
|
|
+ try managedContext.save()
|
|
|
+ //people.append(person)
|
|
|
+ } catch let error as NSError {
|
|
|
+ print("Could not save. \(error), \(error.userInfo)")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "GPS")
|
|
|
+ do {
|
|
|
+ gpsLogData = try managedContext.fetch(fetchRequest)
|
|
|
+ print( "Cnt:" + String(gpsLogData.count) )
|
|
|
+ } catch let error as NSError {
|
|
|
+ print("Could not fetch. \(error), \(error.userInfo)")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|