I am learning how to use the new Swift language (only Swift, no Objective-C). To do it, I want to do a simple view with a map (MKMapView
). I want to find and update the location of the user (like in the Apple Map app).
I tried this, but nothing happened:
import MapKit import CoreLocation class MapView : UIViewController, CLLocationManagerDelegate { @IBOutlet weak var map: MKMapView! var locationManager: CLLocationManager! override func viewDidLoad() { super.viewDidLoad() if (CLLocationManager.locationServicesEnabled()) { locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() } } }
Could you please help me?
You have to override CLLocationManager.didUpdateLocations
(part of CLLocationManagerDelegate) to get notified when the location manager retrieves the current location:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.last{ let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) self.map.setRegion(region, animated: true) } }
NOTE: If your target is iOS 8 or above, you must include the NSLocationAlwaysUsageDescription
or NSLocationWhenInUseUsageDescription
key in your Info.plist to get the location services to work.
100% working, easy steps and tested
Import libraries:
import MapKit import CoreLocation
set delegates:
CLLocationManagerDelegate,MKMapViewDelegate
Take variable:
let locationManager = CLLocationManager()
write this code on viewDidLoad():
self.locationManager.requestAlwaysAuthorization() // For use in foreground self.locationManager.requestWhenInUseAuthorization() if CLLocationManager.locationServicesEnabled() { locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.startUpdatingLocation() } mapView.delegate = self mapView.mapType = .standard mapView.isZoomEnabled = true mapView.isScrollEnabled = true if let coor = mapView.userLocation.location?.coordinate{ mapView.setCenter(coor, animated: true) }
Write delegate method for location:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let locValue:CLLocationCoordinate2D = manager.location!.coordinate mapView.mapType = MKMapType.standard let span = MKCoordinateSpanMake(0.05, 0.05) let region = MKCoordinateRegion(center: locValue, span: span) mapView.setRegion(region, animated: true) let annotation = MKPointAnnotation() annotation.coordinate = locValue annotation.title = "Javed Multani" annotation.subtitle = "current location" mapView.addAnnotation(annotation) //centerMap(locValue) }
Do not forgot to set permission in info.plist
<key>NSLocationWhenInUseUsageDescription</key> <string>This application requires location services to work</string> <key>NSLocationAlwaysUsageDescription</key> <string>This application requires location services to work</string>
It's look like:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With