Skip to content

Instantly share code, notes, and snippets.

@akio0911
Created November 26, 2020 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akio0911/86cd9c70071432a7de64742768d3d6ac to your computer and use it in GitHub Desktop.
Save akio0911/86cd9c70071432a7de64742768d3d6ac to your computer and use it in GitHub Desktop.
import UIKit
import CoreLocation
class ViewController: UIViewController {
@IBOutlet weak var locationLabel: UILabel!
private let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedAlways:
break
case .authorizedWhenInUse:
locationManager.startUpdatingLocation()
case .denied:
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
break
@unknown default:
fatalError("@unknown default")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let coordinate = locations.last!.coordinate
let latitude = coordinate.latitude
let longitude = coordinate.longitude
locationLabel.text = "lat: \(latitude), long: \(longitude)"
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
locationLabel.text = "error: \(error.localizedDescription)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment