Skip to content

Instantly share code, notes, and snippets.

@vesic
Created November 24, 2022 18:09
Show Gist options
  • Save vesic/b1ad3f4682029497974a47b55d95e8df to your computer and use it in GitHub Desktop.
Save vesic/b1ad3f4682029497974a47b55d95e8df to your computer and use it in GitHub Desktop.
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