Created
November 26, 2020 10:26
-
-
Save akio0911/86cd9c70071432a7de64742768d3d6ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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