From 3546083d893e4322f67c7abf0bd9bc348784f39d Mon Sep 17 00:00:00 2001 From: Geoff Donaldson <> Date: Wed, 14 Jul 2021 18:26:41 -0700 Subject: [PATCH] adding heading to image metadata --- camera/ios/Plugin/CameraPlugin.swift | 22 +++++-- camera/ios/Plugin/Locator.swift | 87 ++++++++++++++++++---------- 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/camera/ios/Plugin/CameraPlugin.swift b/camera/ios/Plugin/CameraPlugin.swift index b236efc36..8a35f9a37 100644 --- a/camera/ios/Plugin/CameraPlugin.swift +++ b/camera/ios/Plugin/CameraPlugin.swift @@ -11,6 +11,8 @@ public class CameraPlugin: CAPPlugin { private let defaultDirection = CameraDirection.rear private var currentLocation = CLLocation() + private var currentHeading = CLHeading() + private var imageCounter = 0 @objc override public func checkPermissions(_ call: CAPPluginCall) { @@ -221,11 +223,9 @@ private extension CameraPlugin { } func showPrompt() { - print("Trying to get the Location!!!") Locator.shared.authorize(); - Locator.shared.locate() { result in + Locator.shared.getLocation { result in print("Location Result: \(String(describing: result))") - print(result) switch result { case Locator.Result.Success(let locator): if let location = locator.location { @@ -235,6 +235,18 @@ private extension CameraPlugin { print(error) } } + + Locator.shared.getHeading { result in + print("Heading Result: \(String(describing: result))") + switch result { + case Locator.Result.Success(let locator): + if let heading = locator.heading { + self.currentHeading = heading + } + case Locator.Result.Failure(let error): + print(error) + } + } // Build the action sheet let alert = UIAlertController(title: settings.userPromptText.title, message: nil, preferredStyle: UIAlertController.Style.actionSheet) @@ -396,8 +408,10 @@ private extension CameraPlugin { metadata = asset.imageData } - metadata[kCGImagePropertyGPSDictionary as String] = self.currentLocation.exifMetadata() + metadata[kCGImagePropertyGPSDictionary as String] = self.currentLocation.exifMetadata(heading: self.currentHeading) + print("Meta Data: \(String(describing: metadata))") + // get the result let result = processedImage(from: image, with: metadata) // conditionally save the image diff --git a/camera/ios/Plugin/Locator.swift b/camera/ios/Plugin/Locator.swift index dee741eb7..18f3cd931 100644 --- a/camera/ios/Plugin/Locator.swift +++ b/camera/ios/Plugin/Locator.swift @@ -13,23 +13,20 @@ class Locator: NSObject, CLLocationManagerDelegate { typealias Callback = (Result ) -> Void - var requests: Array = Array () - - var location: CLLocation? { return sharedLocationManager.location } + var locationRequests: Array = Array () + var headingRequests: Array = Array () + var location: CLLocation? { return sharedLocationManager.location } + var heading: CLHeading? { return sharedLocationManager.heading } + var headingCnt: Int = 0 + var locationCnt: Int = 0 + lazy var sharedLocationManager: CLLocationManager = { let newLocationmanager = CLLocationManager() newLocationmanager.delegate = self newLocationmanager.desiredAccuracy = kCLLocationAccuracyBestForNavigation // ... print("sharedLocationManager!!") - - if CLLocationManager.authorizationStatus() == .notDetermined { - newLocationmanager.requestWhenInUseAuthorization() - } else { - newLocationmanager.startUpdatingLocation() - } - return newLocationmanager }() @@ -43,37 +40,67 @@ class Locator: NSObject, CLLocationManagerDelegate { // MARK: - Helpers - func locate(callback: @escaping Callback) { - self.requests.append(callback) - sharedLocationManager.startUpdatingLocation() - sharedLocationManager.startUpdatingHeading() - print("Trying to Locate!!") + func getLocation(callback: @escaping Callback) { + if CLLocationManager.authorizationStatus() == .notDetermined { + sharedLocationManager.requestWhenInUseAuthorization() + } else { + sharedLocationManager.startUpdatingLocation() + self.locationRequests.append(callback) + sharedLocationManager.startUpdatingLocation() + print("Trying to get Location!!") + } + } + + func getHeading(callback: @escaping Callback) { + if (CLLocationManager.headingAvailable()) { + self.headingRequests.append(callback) + sharedLocationManager.headingFilter = 1 + sharedLocationManager.startUpdatingHeading() + print("Trying to get Heading!!") + } } - func reset() { - self.requests = Array () + func resetLocation() { + self.locationRequests = Array () sharedLocationManager.stopUpdatingLocation() } + + func resetHeading() { + self.headingRequests = Array () + sharedLocationManager.stopUpdatingHeading() + } // MARK: - Delegate - func locationManager(_ manager: CLLocationManager, didFailWithError error: NSError) { - print("DID NOT UPDATE LOCATION!!") - print("Location Result: \(String(describing: error))") - for request in self.requests { request(.Failure(error)) } - self.reset() + func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { + print("Location Error: \(String(describing: error))") + for request in self.locationRequests { request(.Failure(error)) } + self.resetLocation() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { - print("DID UPDATE LOCATION!!") print("Location Result: \(String(describing: self.location))") - for request in self.requests { request(.Success(self)) } - self.reset() - } + for request in self.locationRequests { request(.Success(self))} - func headingManger(_ manager: CLLocationManager, didUpdateHeading: [CLHeading]) { - var heading = manager.heading?.magneticHeading - print("DID UPDATE HEADING!!") - print("heading = \(heading)") + if (locationCnt < 4) { + locationCnt += 1 + return + } + locationCnt = 0 + + self.resetLocation() + } + + func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { + print("Heading Result: \(String(describing: heading))") + for request in self.headingRequests { request(.Success(self))} + + if (headingCnt < 4) { + headingCnt += 1 + return + } + headingCnt = 0 + + self.resetHeading() } }