swift - Swift 中的 MapKit

标签 swift mapkit cllocationmanager ios8

我正在尝试在 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

采取所有必要措施确保地理定位​​:

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

最佳答案

已解决。只需添加以下内容:

locationManager.startUpdatingLocation()

几秒钟后,模拟器中一切正常。

关于swift - Swift 中的 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200530/

相关文章:

ios - 更改 mapView Swift 的区域(缩放)

ios - 如何检查用户当前位置是否在位置圈内

ios Google Map - 如何在不缩放到相机的情况下获取我的设备位置

ios - 在 Swift 中将字符串传递回之前的 View Controller 时遇到困难

ios5 - MKMapView NSInvalidArgumentException ios 中的无效区域崩溃

swift - UIBarButtonItem 在作为属性创建时不起作用,但在函数中创建时起作用

iphone - MapView 高于 iOS 应用程序中的所有对象

ios - CLLocationManager 权限

ios - 使用 Swinject 将 Model 类实例注入(inject) Swift 中的 View 类实例

ios - subview 模型的计算属性不会更新父 View 模型的 @ObservedObject