错误消息:类型为[[MKMapView]?没有成员“ showsUserLocation”

时间:2019-05-16 01:18:36

标签: xcode

我正在遵循Mapkit教程以在地图上显示用户位置,但收到错误消息“类型'[MKMapView]的值?”。使用代码“ mapView.showsUserLocation = true”时没有成员“ showsUserLocation””,有什么办法解决吗?

... 导入UIKit 导入MapKit

ViewController类:UIViewController {

private let locationManager = CLLocationManager()
private var currentCoordinate: CLLocationCoordinate2D?

@IBOutlet var mapView: [MKMapView]!
override func viewDidLoad() {
    super.viewDidLoad()
    configureLocationServices()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
   super.didReceiveMemoryWarning()
}

private func configureLocationServices () {
    locationManager.delegate = self
    let status = CLLocationManager.authorizationStatus()

    if status == .notDetermined {
        locationManager.requestAlwaysAuthorization()
    } else if status == .authorizedAlways || status == .authorizedWhenInUse {
        beginLocationUpdates(locationManager: locationManager)        }
}
   private func beginLocationUpdates(locationManager: CLLocationManager) {
    mapView.showsUserLocation = true
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }
private func zoomToLatestLocation(with coordinate: CLLocationCoordinate2D) {
    let zoomRegion = MKCoordinateRegion(center: coordinate, latitudinalMeters: 10000, longitudinalMeters: 10000)
     mapView.setRegion(zoomRegion, animated: true)
}

} 扩展ViewController:CLLocationManagerDelegate {

func locationManager(_管理器:CLLocationManager,didUpdateLocations位置:[CLLocation]){         打印(“获取最新位置”)

    guard let latestLocation = locations.first else { return }

    if currentCoordinate == nil {
        zoomToLatestLocation(with: latestLocation.coordinate)
    }
    currentCoordinate = latestLocation.coordinate  
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    print("The status changed")
    if status == .authorizedAlways || status == .authorizedWhenInUse {
        beginLocationUpdates(locationManager: manager)
    }

...

无论如何,感谢您提供的任何帮助。我真的很感激。

1 个答案:

答案 0 :(得分:0)

请注意,您已将mapView声明为MKMapView数组。那就是方括号的意思:

@IBOutlet var mapView: [MKMapView]!

错误消息表明确实可以,数组没有showsUserLocation方法。

如果只需要一个mapView,则无需将其设置为数组:

@IBOutlet var mapView: MKMapView!
相关问题