以下是我的VC代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
mapView.showsUserLocation = (status == .AuthorizedAlways)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}
我在plist文件中添加了NSLocationWhenInUseUsageDescription
,同时也导入了CoreLocation
和MapKit
框架,但是为什么这个功能不起作用呢?地图上没有显示用户位置,也没有输出用户坐标。在网上和Stack Overflow上都没有找到解决方法。
locationManager
上调用startUpdatingLocation
方法的位置是哪里? - rmaddyfunc locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
中,检查状态是否为AuthorizedWhenInUse
,然后开始更新位置 :) - sloikstartUpdatingLocation
了,但地图上仍然没有显示用户位置。谢谢 @rmaddy - Ryan Westcott