【问题标题】:Error message: Value of type '[MKMapView]?' has no member 'showsUserLocation'错误消息:“[MKMapView]?”类型的值没有成员“showsUserLocation”
【发布时间】:2019-05-16 01:18:35
【问题描述】:

我正在按照 Mapkit 教程在地图上显示用户位置,但我收到错误消息“'[MKMapView] 类型的值?'使用代码“mapView.showsUserLocation = true”时没有成员“showsUserLocation”有没有办法解决这个问题?

... 导入 UIKit 导入 MapKit

类视图控制器: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)
}

} 扩展视图控制器:CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didUpdateLocations 位置: [CLLocation]) { print("得到了最新的位置")

    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)
    }

...

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

【问题讨论】:

    标签: xcode


    【解决方案1】:

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

    @IBOutlet var mapView: [MKMapView]!
    

    错误消息说确实是的,数组没有showsUserLocation 方法。

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

    @IBOutlet var mapView: MKMapView!
    

    【讨论】:

    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多