Saturday, September 27, 2014

MapKit - Show the current location

Update: April 27, 2017 with Xcode 8.3.2 (Swift 3.1).

1. Add the mapkit in the storyboard and edit the code as below:

import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var theMap: MKMapView!

2. Edit the info.plist file by adding these two items:

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription (Update: This is not essential)

3. Edit the code as below:

Update:
April 27, 2017 with Xcode 8.3.2 (Swift 3.1)

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var theMap: MKMapView!
    
    //Declare locationManager here to prevent location access message from disappearance.
    var locationManager : CLLocationManager!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        
        theMap.delegate = self
        theMap.mapType = MKMapType.standard
        theMap.showsUserLocation = true
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if(!locations.isEmpty)
        {
            let myLocation = locations[0] as CLLocation
            
            theMap.setRegion(MKCoordinateRegionMake(myLocation.coordinate, MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)), animated: true)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Original Post:
September 27, 2014 with Xcode 6 (Swift 1)

    var locationManager : CLLocationManager!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        
        theMap.delegate = self
        theMap.mapType = MKMapType.Standard
        theMap.showsUserLocation = true


    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        
        if (!locations.isEmpty)
        {


            let myLocation  = locations[0] as! CLLocation
            


            theMap.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude), MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)), animated: true)
        }

    }

2 comments:

  1. code is not for display current location
    Wrong code

    ReplyDelete
    Replies
    1. Updated with Xcode 8.3.2 (Swift 3.1).

      Delete