【问题标题】:Why are my annotations not showing?为什么我的注释没有显示?
【发布时间】:2018-01-06 08:53:26
【问题描述】:
import UIKit
import MapKit
//import Firebase

//    alle UIViewController() functions
class ThirdViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        requestLocationAccess()

        //Regio instellen
        var newRegion:MKCoordinateRegion = MKCoordinateRegion()
        newRegion.center.latitude = -25.106303;
        newRegion.center.longitude = 133.587542;
        newRegion.span.latitudeDelta = 30;
        newRegion.span.longitudeDelta = 30;

        self.mapView.setRegion(newRegion, animated: true)
        self.mapView.delegate = self

        if let cityDetailsPath = Bundle.main.path(forResource: "Hostels", ofType: "plist"){
            if let cityDetails = NSArray(contentsOfFile: cityDetailsPath) as? [[String: String]] {
                for city in cityDetails
                {
                    guard let latStr = city["latitude"] else { continue }
                    guard let lonStr = city["longitude"] else { continue }
                    guard let titleStr = city["title"] else { continue }
                    guard let subtitleStr = city["subTitle"] else { continue }

                    if let lat = Double(latStr) {
                        if let lon = Double(lonStr) {
                            let title = titleStr
                            let subTitle = subtitleStr

                            let annotation = PinAnnotation(coordinate: CLLocationCoordinate2DMake(lat,lon), title: title, subtitle: subTitle)

                            self.mapView.addAnnotation(annotation)
                        }
                    }
                }
            }
        }
    }

    // Vraag locatie toestemming
    func requestLocationAccess()
    {
        let status = CLLocationManager.authorizationStatus()

        switch status
        {
        case .authorizedAlways, .authorizedWhenInUse:
            return

        case .denied, .restricted:
            print("location access denied")

        default:
            locationManager.requestWhenInUseAuthorization()
        }
    }

    func showLocations(location:PinAnnotation){

    }
}

//    alle UISearchBarDelegate functions
extension ThirdViewController: UISearchBarDelegate {

}

//    alle MKMapViewDelegate functions
extension ThirdViewController: MKMapViewDelegate {
    func createViewAnnotationForMap(mapView:MKMapView, annotation:MKAnnotation)->MKAnnotationView {
        _ = "PinAnnotation"
        if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "PinAnnotation") {
            return annotationView
        }
        else
        {
            let returnedAnnotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier:"PinAnnotation")
            returnedAnnotationView.animatesDrop = true
            returnedAnnotationView.canShowCallout = true
            return returnedAnnotationView

        }

    }
}

【问题讨论】:

  • self.mapView.addAnnotation(annotation) 被执行了吗?
  • 据我所知是的
  • 你能发布你的 Hostels.plist 内容吗?
  • 检查你所有的字典数组是否对我来说都是[String:String] 这工作得很好

标签: ios annotations mkmapview mkannotation


【解决方案1】:
let returnedAnnotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier:"PinAnnotation")
returnedAnnotationView.animatesDrop = true
returnedAnnotationView.canShowCallout = true
return returnedAnnotationView

【讨论】:

  • 请不要只写代码答案,试着解释你在做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多