Contents

desp0o/izzilocationkit

IzziLocationKit is a lightweight Swift package designed for easy integration and seamless location handling in Swift apps. With it, you can get your current location and coordinates. You can also get full information about your location, like the country name, ISO code, and posta

1️⃣ First of all request location access by adding the key

```swift
Privacy - Location When In Use Usage Description’ to your Info.plist ✅
```

### Get current coordinates or location.

```swift
import SwiftUI
import IzziLocationKit

struct ContentView: View {
  @State private var locationManager = IzziLocationKit()
  
  var body: some View {
    VStack {
      if let coordinates = locationManager.currentCoordinates {
        Text("\(coordinates.latitude)")
        Text("\(coordinates.longitude)")
      }
      
      if let location = locationManager.currentLocation {
        Text("\(location)")
      }
    }
    .task {
      locationManager.getLocation()
    }
  }
}
```
#### If necessary, update the current location and coordinates continuously by using the `locationMode`. 

```swift
init() {
  locationManager.locationMode = .continuous
}
```
### Convert Place Name to Coordinates
```swift
var coordinates: CLLocationCoordinate2D? = nil
var errorMessage: String = ""

Task { 
  do {
    let response = try await locationManager.getCoordinatesFromPlace(addressName: "washington")
    coordinates = response
  } catch {
    errorMessage = error.localizedDescription
  }
}
```

### Get full info of location
use any location - `CLLocation` to get full info

```swift
var info: FullLocationInfoModel? = nil
var errorMessage: String = ""
      
let location = CLLocation(latitude: -16.697460, longitude: 179.257957)
      
Task { @MainActor in
  do {
    let response = try await locationManager.getFullLocationInfo(with: location)
    info = response
  } catch {
    errorMessage = error.localizedDescription
  }
}
```
### Get route info from Mapkit
#### ❗️Apple Maps may not function correctly in all countries and locations.❗️

#### With Coordinates - `CLLocationCoordinate2D`
```swift
var route: RouteModel? = nil
var errorMessage: String = ""
      
let locationOne = CLLocationCoordinate2D(latitude: 39.724772, longitude: -105.104127)
let locationTwo = CLLocationCoordinate2D(latitude: 39.739186, longitude: -104.779517)

Task { 
  do {
    let response = try await locationManager.getRouteWithLocations(from: locationOne, to: locationTwo, with: .automobile)
    route = response
  } catch {
    errorMessage = error.localizedDescription
  }
}
```
#### With place Name - `String`

```swift
var route: RouteModel? = nil
var errorMessage: String = ""

Task { 
  do {
    let response = try await locationManager.getRouteWithPlaceName(from: "New York", to: "Chicago", with: .automobile)
    route = response
  } catch {
    errorMessage = error.localizedDescription
  }
}
```

### Calculate Distance _(in meters)_
#### ⚠️ This distance, known as the straight-line distance or “as-the-crow-flies” distance, is technically referred to as the great-circle distance. It represents the shortest path between two points on the surface of a sphere. 

#### With place Name - `String`
```swift
var distance: Double? = nil
var errorMessage: String = ""

Task { 
  do {
    let response = try await locationManager.getDistanceBetweenPlaces(from: "New York", to: "Chicago")
    distance = response
  } catch {
    errorMessage = error.localizedDescription
  }
}
```

#### With locations
```swift
let locationOne = CLLocation(latitude: 39.724772, longitude: -105.104127)
let locationTwo = CLLocation(latitude: 39.739186, longitude: -104.779517)

let distance: Double = locationManager.getDistanceBetweenLocations(from: locationOne, to: locationTwo)        
print("Distance is: \(distance)m")

//Distance is: 27874.0m
```

### How to use `isError` and `errorMessage`
```swift
import SwiftUI
import IzziLocationKit

struct ContentView: View {
  @State private var locationService = IzziLocationKit()

  var body: some View {
    VStack(spacing: 20) {
      if let coordinates = locationService.currentCoordinates {
        Text("Latitude: \(coordinates.latitude)")
        Text("Longitude: \(coordinates.longitude)")
      } else {
        Text("Getting location...")
      }

      if locationService.isError {
        Text("Error: \(locationService.errorMessage)")
          .foregroundColor(.red)
      }
    }
    .task {
      locationService.getLocation()
    }
  }
}

```

# Preferences ⚙️
### Location State 
| Parameter         | Type                            | Description                                          | Default |
|------------------|----------------------|-------------------------------------------------------|---------|
| `currentLocation`       | `CLLocation?`             | Current location                                     | nil     |
| `currentCoordinates`    | `CLLocationCoordinate2D?` | Current coordinates.                                 | nil     |
| `locationAccessStatus`  | `CLAuthorizationStatus`   | The current authorization status for accessing location services. | .notDetermined     |
| `isDenied`              | `Bool`                    | Indicates whether the user has denied access to location services.              | false     |
| `isError `              | `Bool`                    | Indicates whether an error has occurred. Useful for triggering UI feedback. .              | false     |
| `errorMessage `              | `String`                    | Contains the error message to be displayed in the UI when isError is true.              | ""     |

### Configuration 
| Parameter         | Type                            | Description                                          | Default |
|------------------|----------------------|-------------------------------------------------------|---------|
| `locationMode`          | `IzziLocationMode`        | Determines whether location updates are continuous or one-time.                | .oneTime     |
| `desiredAccuracy`       | `CLLocationAccuracy`      | The desired accuracy level for location updates.                                    | kCLLocationAccuracyBest   |
| `distanceFilter`        | `CLLocationDistance`      | The minimum distance (in meters) the device must move before an update.               | 10   |
| `activityType`          | `CLActivityType`          | Specifies the type of activity the location updates are being used for.                         | .other   |

#### to use any of this configuration you just need to apply it to your locationManager.
```swift
@State private var locationManager = IzziLocationKit()
  
init() {
  locationManager.locationMode = .continuous
  locationManager.desiredAccuracy = kCLLocationAccuracyReduced
  locationManager.distanceFilter = 100
  locationManager.activityType = .fitness
}
```
# Models 🧩

<details>
  <summary><strong>FullLocationInfoModel</strong> – Click to expand ↕️</summary>

```swift
public struct FullLocationInfoModel {
  public let country: String
  public let iso: String
  public let postalCode:  String
  public let name: String
  public let administrativeArea: String
  public let areasOfInterest: [String]
  public let inlandWater: String
  public let locality: String
  public let ocean: String
  public let timeZone: String
  public let regionIdentifier: String
  public let subAdministrativeArea: String
  public let subLocality: String
  public let subThoroughfare: String
  public let locationInfo: LocationInfoModel
}
```

</details>

<details>
  <summary><strong>LocationInfoModel</strong> – Click to expand ↕️</summary>

```swift
public struct LocationInfoModel {
  public let latitude: Double?
  public let longitude: Double?
  public let timestamp: Date?
  public let horizontalAccuracy: Double?
  public let speed: Double?
  public let verticalAccuracy: Double?
  public let course: Double?
  public let ellipsoidalAltitude: Double?
  public let floor: CLFloor?
  public let speedAccuracy: Double?
}

```

</details>

<details>
  <summary><strong>RouteModel</strong> – Click to expand ↕️</summary>

```swift
public struct RouteModel {
  public let advisoryNotices: [String]
  public let distanceInMeters: Double
  public let expectedTravelTime: TimeInterval
  public let hasHighways: Bool
  public let hasTolls: Bool
  public let routeName: String
  public let steps: [String]
  public let polyline: MKPolyline
}

```

</details>

Installation via Swift Package Manager 🖥️

  • Open your project.
  • Go to File → Add Package Dependencies.
  • Enter URL: https://github.com/Desp0o/IzziLocationKit.git
  • Click Add Package.

Contact 📬

  • Email: tornike.despotashvili@gmail.com
  • LinkedIn: https://www.linkedin.com/in/tornike-despotashvili-250150219/
  • github: https://github.com/Desp0o

Package Metadata

Repository: desp0o/izzilocationkit

Default branch: main

README: README.md