0

I'm trying work with Map Kit in Swift. I try to display the area on the map, one pin and the current position. All display fine, exclude current position - it not displayed.

This is a sample code:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mainMapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    var userLocation = MKUserLocation()
    println("Location: \(userLocation.location)")

    var objectLatitude = 53.204526
    var objectLongitude = 50.111751

    var currentLatitude = 53.203715
    var currentLongitude =  50.160374

    var latDelta = 0.01
    var longDelta = 0.01

    var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(currentLatitude, currentLongitude)
    var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation, currentLocationSpan)
    self.mainMapView.setRegion(currentRegion, animated: true)

    var objectLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(objectLatitude, objectLongitude)
    var objectAnnotation = MKPointAnnotation()
    objectAnnotation.coordinate = objectLocation
    objectAnnotation.title = "St. George's Church"
    objectAnnotation.subtitle = "Church of the Great Martyr St. George"
    self.mainMapView.addAnnotation(objectAnnotation)
}

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

Line println("Location: \(userLocation.location)") display Location: nil

All necessary actions to ensure geolocation are made​​:

enter image description here enter image description here enter image description here

Alexey Nakhimov
  • 2,673
  • 8
  • 34
  • 49
  • Note that you should not be creating instances of `MKUserLocation` yourself (eg. `var userLocation = MKUserLocation()`). The map view will create an instance of that internally. You should be reading the location using the userLocation property or in the didUpdateUserLocation or didUpdateLocations delegate methods. –  Aug 08 '14 at 11:24
  • Thanks for all - I was solved this problem. Could you help me with another problem in this code? Here's a link to the question: http://stackoverflow.com/questions/25202613/mapkit-in-swift-part-2 – Alexey Nakhimov Aug 08 '14 at 11:38

2 Answers2

1

Solved. Simply add this:

locationManager.startUpdatingLocation()

After few second all working fine in simulator.

Alexey Nakhimov
  • 2,673
  • 8
  • 34
  • 49
0

NSLocationAlwaysUsageDescription should be a String containing the text which is displayed to the user. You have declared it as a Boolean.

Are you running this in simulator? There is an issue with XCode 6 Beta 5: Location simulation is not working (see relealse notes):

Location functionality may not work in the Simulator. (17858614)

zisoft
  • 22,770
  • 10
  • 62
  • 73
  • It's true the key is not a boolean and should actually be set to a reason the user will understand. But technically it only requires that it be set to some value so "YES" works (so will "NO" or "foo"). –  Aug 08 '14 at 11:22