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
| import UIKit import CoreLocation
class ViewController: UIViewController {
let locationManager = CLLocationManager()
@IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.requestLocation() }
}
extension ViewController: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.last { let lat = location.coordinate.latitude let lon = location.coordinate.longitude print(lat) print(lon) self.label1.text = String(lat) self.label2.text = String(lon) } } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print(error) } }
|