import UIKit
import CoreLocation
class LocationTrackerViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let latitudeLabel = UILabel()
let longitudeLabel = UILabel()
let startButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
latitudeLabel.text = "Latitude: ---.------"
longitudeLabel.text = "Longitude: ---.------"
startButton.setTitle("Start Tracking", for: .normal)
latitudeLabel.translatesAutoresizingMaskIntoConstraints = false
longitudeLabel.translatesAutoresizingMaskIntoConstraints = false
startButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(latitudeLabel)
view.addSubview(longitudeLabel)
view.addSubview(startButton)
NSLayoutConstraint.activate([
latitudeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
latitudeLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100),
longitudeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
longitudeLabel.topAnchor.constraint(equalTo: latitudeLabel.bottomAnchor, constant: 20),
startButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
startButton.topAnchor.constraint(equalTo: longitudeLabel.bottomAnchor, constant: 40)
])
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
startButton.addTarget(self, action: #selector(startTracking), for: .touchUpInside)
checkLocationAuthorization()
}
func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted, .denied:
showPermissionDeniedAlert()
case .authorizedWhenInUse, .authorizedAlways:
// Permission granted, do nothing yet
break
@unknown default:
break
}
}
@objc func startTracking() {
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
} else {
showPermissionDeniedAlert()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
latitudeLabel.text = String(format: "Latitude: %.6f", location.coordinate.latitude)
longitudeLabel.text = String(format: "Longitude: %.6f", location.coordinate.longitude)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorization()
}
func showPermissionDeniedAlert() {
let alert = UIAlertController(title: "Location Permission Denied", message: "Please enable location permissions in Settings to use this feature.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
}
This app uses Core Location to get the user's current position. We first ask for permission to access location data. If permission is denied, we show an alert to inform the user.
The startButton triggers location updates. When the location changes, the delegate method updates the latitude and longitude labels with the new coordinates.
We use CLLocationManagerDelegate to handle location updates and authorization changes. The UI is simple and centered, showing the coordinates clearly.