Reputation: 1
I'm following a Mapkit tutorial to display the user location on a map but I get the error message "Value of type '[MKMapView]?' has no member 'showsUserLocation'" when using the code "mapView.showsUserLocation = true" is there any way of resolving this?
... import UIKit import MapKit
class 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)
}
} extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("Did get latest location")
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)
}
...
Anyway, thanks for any help you can offer. I really appreciate it.
Upvotes: 0
Views: 1446
Reputation: 9324
Note that you’ve declared mapView
as being an array of MKMapView
s. That’s what the square brackets mean:
@IBOutlet var mapView: [MKMapView]!
The error message says that yes indeed, arrays do not have a showsUserLocation
method.
If you need only one mapView
, no need to make it an array:
@IBOutlet var mapView: MKMapView!
Upvotes: 1