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: