Created
November 24, 2022 18:09
-
-
Save vesic/b1ad3f4682029497974a47b55d95e8df to your computer and use it in GitHub Desktop.
This file contains hidden or 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, CLLocationManagerDelegate { | |
let locmanager = CLLocationManager() | |
let locationLabel: UILabel = { | |
let label = UILabel() | |
label.textColor = .darkGray | |
label.font = UIFont.boldSystemFont(ofSize: 32) | |
label.textAlignment = .center | |
label.numberOfLines = 0 | |
label.translatesAutoresizingMaskIntoConstraints = false | |
return label | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupUI() | |
self.locmanager.delegate = self | |
locmanager.requestWhenInUseAuthorization() | |
locmanager.startUpdatingLocation() | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
self.locmanager.stopUpdatingLocation() | |
} | |
} | |
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
let loc = locations.last! | |
let coord = loc.coordinate | |
locationLabel.text = """ | |
You are at | |
\(coord.latitude) | |
\(coord.longitude) | |
""" | |
print("You are at \(coord.latitude) \(coord.longitude)") | |
} | |
func setupUI() { | |
view.addSubview(locationLabel) | |
NSLayoutConstraint.activate([ | |
locationLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), | |
locationLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor), | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment