【发布时间】:2016-02-06 12:23:43
【问题描述】:
我只是想使用 GoogleMaps API 在我的 MapViewController 中获取用户位置。 我环顾四周,无法弄清楚为什么我不断出现致命错误:在这个特定位置展开时意外发现 nil 和 Optional 值。
这是我的代码:
class MapViewController: UIViewController {
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let camera = GMSCameraPosition.cameraWithLatitude(-34.9290, longitude:138.6010, zoom:10)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
mapView.mapType = kGMSTypeNormal
print("Calling addMarker")
// addMarker()
self.view = mapView
}
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
这是我得到错误的地方:
我知道mapView.myLocationEnabled 返回 nil 但不明白为什么/如何。
【问题讨论】:
标签: ios swift mkmapview cllocationmanager