Skip to content

Commit

Permalink
adding heading to image metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Donaldson committed Jul 15, 2021
1 parent 66b6565 commit 3546083
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 34 deletions.
22 changes: 18 additions & 4 deletions camera/ios/Plugin/CameraPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
87 changes: 57 additions & 30 deletions camera/ios/Plugin/Locator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ class Locator: NSObject, CLLocationManagerDelegate {

typealias Callback = (Result <Locator>) -> Void

var requests: Array <Callback> = Array <Callback>()

var location: CLLocation? { return sharedLocationManager.location }
var locationRequests: Array <Callback> = Array <Callback>()
var headingRequests: Array <Callback> = Array <Callback>()

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
}()

Expand All @@ -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 <Callback>()
func resetLocation() {
self.locationRequests = Array <Callback>()
sharedLocationManager.stopUpdatingLocation()
}

func resetHeading() {
self.headingRequests = Array <Callback>()
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()
}
}

0 comments on commit 3546083

Please sign in to comment.