Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/przemek-jablonski/earendil
Fetch user location without asking for permission 👺. All  platforms.
https://github.com/przemek-jablonski/earendil
apple geolocation ios location macos multiplatform swift
Last synced: about 2 months ago
JSON representation
Fetch user location without asking for permission 👺. All  platforms.
- Host: GitHub
- URL: https://github.com/przemek-jablonski/earendil
- Owner: przemek-jablonski
- License: mit
- Created: 2021-08-20T21:11:46.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-08T14:35:15.000Z (over 1 year ago)
- Last Synced: 2023-06-08T15:32:47.579Z (over 1 year ago)
- Topics: apple, geolocation, ios, location, macos, multiplatform, swift
- Language: Swift
- Homepage:
- Size: 879 KB
- Stars: 4
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Earendil
Swift Package for fetching approximate user location without asking for their permission 👺.
Get their country, subregion or continent with one method call. This includes country names, their coordinates and even `MKCoordinateRegion` property, instantly usable with your `MapKit` views. Perfect for setting your Map's location for the first time, if you don't need GPS permission otherwise.
100% App Store safe, no-private APIs used.
## Usage
Get the location at any time:
```swift
let userLocation = Earendil.inferredLocationuserLocation.country.name // "United States of America"
userLocation.country.region.center // (37.09, -95.7)
userLocation.country.region.span // (60, 60)
```Use the inferred location for your needs:
```swift
let userLocation = Earendil.inferredLocation// use location data as the first-time region displayed by the map
mapView.setRegion(userLocation.country.coordinateRegion, animated: true)// use location title (ENG only) as a content of any label in your app
navigationItem.title = userLocation.region.name// use location for analytics purposes
FirebaseAnalytics.logEvent(UserLocationEvent(userLocation))
```## What? How come? Why?
It's super simple - the idea is to fetch current OS system Locale (`NSLocale.current`), cast it back to Foundation's `NSLocale` and retrieve the two-character `countryCode` key. This key is mapped to internal json dataset, which contains names and coordinates for every worlds-location. That way there is no need to ask for any location permission. You can check it out right here: [InferredLocationProvider.swift](Sources/Earendil/Internals/InferredLocationProvider.swift#L22).This approach has some consequences, mainly:
- Returned location (even for most granular level, `country`, is really very broad)
- Users travelling to another country won't update Earendil's location (which may even be desired).
- Setting location-agnostic Locale (such as `eu` instead of `eu_ES` or `en` instead of `en_US`) will prevent Earendil from getting proper location.## Installation
Available through Swift Package Manager - open up the xcode, type this repos URL and you are good to go.
## Platform Availability
 iOS 14.0
 iPadOS 14.0
 macOS 11.0 Big Sur
 watchOS 7.0
 tvOS 14.0also compliant with  Mac Catalyst