Не удается показать пин-код аннотации на MKMapview Swift - PullRequest
0 голосов
/ 15 мая 2018

Я пытаюсь показать пин-код аннотации для текущего местоположения.Для этого я написал следующий код

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    mapView.layer.cornerRadius = 8.0
    imageView.layer.cornerRadius = 8.0

    //mapview settings
    mapView.mapType = MKMapType.standard
    mapView.isZoomEnabled = true
    mapView.isZoomEnabled = true

    // Or, if needed, we can position map in the center of the view
    mapView.center = view.center
    mapView.delegate = self

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    determineCurrentLocation()
}

func determineCurrentLocation()
{
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        //locationManager.startUpdatingHeading()
        locationManager.startUpdatingLocation()
    }
}
//Mapview Delegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0] as CLLocation

    // Call stopUpdatingLocation() to stop listening for location updates,
    // other wise this function will be called every time when user location changes.
    //manager.stopUpdatingLocation()

    let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    mapView.setRegion(region, animated: true)

    // Drop a pin at user's Current Location
    let myAnnotation: MKPointAnnotation = MKPointAnnotation()
    myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
    myAnnotation.title = "Current location"
    mapView.addAnnotation(myAnnotation)
}

func locationManager(manager: CLLocationManager, didFailWithError error: Error)
{
    print("Error \(error)")
}

Но, это ничего не показывает в просмотре карты.Есть предложения?

enter image description here

1 Ответ

0 голосов
/ 15 мая 2018

Вам необходимо добавить NSLocationAlwaysUsageDescription ключ в Info.plist с сообщением, которое будет отображаться в приглашении.

enter image description here

...