我正在尝试在 Swift 中使用 Map Kit。我尝试在 map 上显示该区域、一根图钉和当前位置。全部显示正常,排除当前位置 - 它不显示。
这是示例代码:
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()
}
}
行 println("位置:\(userLocation.location)")
显示 位置:nil
采取所有必要措施确保地理定位:
最佳答案
已解决。只需添加以下内容:
locationManager.startUpdatingLocation()
几秒钟后,模拟器中一切正常。
关于swift - Swift 中的 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200530/