Skip to content

Instantly share code, notes, and snippets.

@alexnikol
Last active May 23, 2020
Embed
What would you like to do?
Get User Location and Direction
import UIKit
import CoreLocation
class ViewController: UIViewController {
private let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
let point = CLLocationCoordinate2D(latitude: location.coordinate.latitude,
longitude: location.coordinate.longitude)
print("Location \(point), Direction angle \(location.course)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment