MyLocation не работает в приложении (SWIFT3) Map Kit

Я пытаюсь узнать свое текущее местоположение, но мое приложение этого не сделает:(Не могли бы вы помочь мне определить, в чем дело? Приложение работает без ошибок.

импорт UIKit импорт MapKit импорт CoreLocation

Класс MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet var mapView:MKMapView!

let locationManager = CLLocationManager()

var restaurant:Restaurant!

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.delegate = self

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.startUpdatingLocation()

    self.mapView.showsUserLocation = true

    // Convert address to coordinate and annotate it on map
    let geoCoder = CLGeocoder()
    geoCoder.geocodeAddressString(restaurant.location, completionHandler: { placemarks, error in
        if error != nil {
            print(error)
            return
        }

        if let placemarks = placemarks {
            // Get the first placemark
            let placemark = placemarks[0]

            // Add annotation
            let annotation = MKPointAnnotation()
            annotation.title = self.restaurant.name
            annotation.subtitle = self.restaurant.type

            if let location = placemark.location {
                annotation.coordinate = location.coordinate

                // Display the annotation
                self.mapView.showAnnotations([annotation], animated: true)
                self.mapView.selectAnnotation(annotation, animated: true)
            }
        }

    })

    // Map customization
    mapView.showsCompass = true
    mapView.showsScale = true
    mapView.showsTraffic = true

    // Set the MKMapViewDelegate
    mapView.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.
}

// MARK: - MKMapViewDelegate methods


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last

    let center = CLLocationCoordinate2D(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

    self.mapView.setRegion(region, animated: true)

    self.locationManager.stopUpdatingLocation()
}



func locationManager(_ manager: CLLocationManager, didFailWithError: Error)
{
    print("Errors:")
}

0 ответов

Другие вопросы по тегам