Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Current Location and Update Location in MKMapView in Swift

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?

like image 897
PMIW Avatar asked Aug 22 '14 14:08

PMIW


2 Answers

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.

like image 190
zisoft Avatar answered Sep 22 '22 15:09

zisoft


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:

enter image description here

like image 31
Mr.Javed Multani Avatar answered Sep 23 '22 15:09

Mr.Javed Multani