简体   繁体   English

IOS 地图引脚不显示?

[英]IOS Map Pins are not Showing?

I am struggling with my iOS code.我正在为我的 iOS 代码苦苦挣扎。 The overall goal of the project is to show the pins on the map, I've tried multiple ways to make the pins appear on the map such as the below, I am not sure what I am doing wrong.该项目的总体目标是在地图上显示图钉,我尝试了多种方法使图钉出现在地图上,如下所示,我不确定我做错了什么。 The code runs fine, the pins are not appearing.代码运行良好,引脚没有出现。 I've made changes to my functions but seem to get the same error.我对我的功能进行了更改,但似乎遇到了同样的错误。 Any insight on what I may be missing?关于我可能缺少什么的任何见解?

"import UIKit import MapKit "导入 UIKit 导入 MapKit

class MapViewController: UIViewController, MKMapViewDelegate {类 MapViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var logout: UIBarButtonItem!
@IBOutlet weak var refresh: UIBarButtonItem!
@IBOutlet weak var pinDrop: UIBarButtonItem!


var studentLocation = [StudentInformation]()
var annotations = [MKPointAnnotation]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.
}

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

@IBAction func refresh(_ sender: UIBarButtonItem) {
    getStudentPins ()
}
@IBAction func logout(_ sender: UIBarButtonItem) {
    self.activityIndicator.startAnimating()
    UdacityClient.logout {
        DispatchQueue.main.async {
            self.dismiss(animated: true, completion: nil)
            self.activityIndicator.stopAnimating()
        }
    }
}

@IBAction func addLocation(_ sender: Any) {
    performSegue(withIdentifier: "addLocation", sender: nil)
}

func getStudentPins () {
    self.activityIndicator.startAnimating()
    UdacityClient.getStudentLocations() { locations, error in
        self.mapView.removeAnnotations(self.annotations)
        self.annotations.removeAll()
        self.studentLocation = locations ?? []
        for dictionary in locations ?? [] {
            let latitude = CLLocationDegrees(dictionary.latitude ?? 0.0)
            let longitude = CLLocationDegrees(dictionary.longitude ?? 0.0)
            let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
            let firstName = dictionary.firstName
            let lastName = dictionary.lastName
            let mediaURL = dictionary.mediaURL
            let annotation = MKPointAnnotation()
            annotation.coordinate = coordinate
            annotation.title = "\(firstName) \(lastName)"
            annotation.subtitle = mediaURL
            self.annotations.append(annotation)
        }
        DispatchQueue.main.async {
            self.mapView.addAnnotations(self.annotations)
            self.activityIndicator.stopAnimating()
        }
    }
}

private func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotation? {
    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.pinTintColor = .green
        pinView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }
    else {
        pinView!.annotation = annotation
    }
    return pinView?.annotation
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        if let toOpen = view.annotation?.subtitle {
            openLink(toOpen ?? "")
        }
    }
}

}" }"

They're called annotations in the MapKit and you should instantiate them like so:它们在MapKit被称为annotations ,您应该像这样实例化它们:

let annotation = MKPointAnnotation() 

then in the viewDidLoad() method just set the coordinates and add them to the map like:然后在viewDidLoad()方法中设置坐标并将它们添加到地图中,如下所示:

annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11)
mapView.addAnnotation(annotation)

The numbers are your coordinates.数字是你的坐标。 Try by adding sample coordinates on the map first and then add all coordinates which you are getting from the client.尝试先在地图上添加示例坐标,然后添加您从客户端获得的所有坐标。

you can also add more information using:您还可以使用以下方法添加更多信息:

annotation.title = "Your text here"
//You can also add a subtitle that displays under the annotation such as
annotation.subtitle = "One day I'll go here..."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM