-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationManager.swift
57 lines (45 loc) · 1.59 KB
/
LocationManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// LocationManager.swift
// WeatherOnMap
//
// Created by Mustafa on 4/13/24.
//
import CoreLocation
import Foundation
class LocationManager: NSObject, CLLocationManagerDelegate {
static let shared = LocationManager()
let manager = CLLocationManager()
var completion: ((CLLocation) -> Void)?
public func getUserLocation(completion: @escaping ((CLLocation) -> Void)) {
self.completion = completion
manager.requestWhenInUseAuthorization()
manager.delegate = self
manager.startUpdatingLocation()
}
public func resolveLocationName(with location: CLLocation,
completion: @escaping (String?) -> Void) {
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, preferredLocale: .current) { placemarks, error in
guard let place = placemarks?.first, error == nil else {
completion(nil)
return
}
//print(place)
var name = ""
if let locality = place.locality {
name += locality
}
if let adminRegion = place.administrativeArea {
name += ", \(adminRegion)"
}
completion(name)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
completion?(location)
manager.stopUpdatingLocation()
}
}