-
Notifications
You must be signed in to change notification settings - Fork 0
fix(ios): geotag in-app camera captures (#266) #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import CoreLocation | ||
| import Foundation | ||
|
|
||
| /// Provides the device's current location for geotagging in-app camera captures. | ||
| /// | ||
| /// The photo-library path already reads GPS from each photo's EXIF metadata | ||
| /// (`PhotoService.extractEXIF`), but photos captured with the in-app camera are | ||
| /// bare pixels with no embedded location. This service supplies the device's | ||
| /// current coordinate at capture time so camera photos get the same GPS signal | ||
| /// the range-prior pipeline (and future on-device model) relies on. | ||
| /// | ||
| /// Uses the modern `CLLocationUpdate.liveUpdates()` async sequence (iOS 17+) | ||
| /// rather than the delegate pattern, which keeps it clean under Swift 6 strict | ||
| /// concurrency. When-in-use authorization only; no background tracking. | ||
| /// | ||
| /// Usage: call `start()` when the camera opens, read `latestCoordinate` when a | ||
| /// photo is taken, and `stop()` when the capture flow closes. | ||
| @MainActor | ||
| final class LocationService: ObservableObject { | ||
| /// Most recent location fix, if any. | ||
| @Published private(set) var currentLocation: CLLocation? | ||
|
|
||
| /// Manager retained only to request authorization (no delegate is used). | ||
| private let manager = CLLocationManager() | ||
| private var updatesTask: Task<Void, Never>? | ||
|
|
||
| /// Convenience: the latest coordinate as (lat, lon), or nil if unavailable. | ||
| var latestCoordinate: (lat: Double, lon: Double)? { | ||
| guard let coord = currentLocation?.coordinate, | ||
| CLLocationCoordinate2DIsValid(coord) | ||
| else { return nil } | ||
| return (coord.latitude, coord.longitude) | ||
| } | ||
|
|
||
| /// Request when-in-use permission (if needed) and begin streaming location. | ||
| /// Safe to call repeatedly; a second call is a no-op while already running. | ||
| func start() { | ||
| if manager.authorizationStatus == .notDetermined { | ||
| manager.requestWhenInUseAuthorization() | ||
| } | ||
| guard updatesTask == nil else { return } | ||
| updatesTask = Task { [weak self] in | ||
| do { | ||
| for try await update in CLLocationUpdate.liveUpdates() { | ||
| guard let self else { return } | ||
| if let location = update.location { | ||
| self.currentLocation = location | ||
| } | ||
| } | ||
| } catch { | ||
| // Non-fatal: capture still works, photo just won't be geotagged. | ||
| } | ||
| // Clear our own handle when the stream ends/throws, but only if a | ||
| // newer start() hasn't already replaced it (avoids clobbering a | ||
| // restart). Cancellation is handled by stop(). | ||
| if let self, !Task.isCancelled { | ||
| self.updatesTask = nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Stop location updates (call when the capture flow closes to save battery). | ||
| func stop() { | ||
| updatesTask?.cancel() | ||
| updatesTask = nil | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.