当视图加载时,为什么我的MKPointAnnotation不显示?

时间:2016-11-11 06:19:45

标签: ios swift mkmapview coordinates cgpoint

在我的应用中,用户会保存一些数据,包括地图坐标。在我的代码中,一个引脚被删除在保存的地图坐标处。这是我的代码 -

override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView.delegate = self

    // Data loading
    itemNameTextField.delegate = self
    itemDescriptionLabel.delegate = self
    itemLocationTextView.delegate = self

    // Press recognizer

    if let item = item {

        itemNameTextField.text = item.itemName
        itemDescriptionLabel.text = item.itemDescription
        itemLocationTextView.text = item.itemPlace

        let dropPin = MKPointAnnotation()
        dropPin.coordinate = mapView.convert(item.mapPoint, toCoordinateFrom: mapView)
        dropPin.title = "Location of \(item.itemName)"
        self.mapView.addAnnotation(dropPin)
        print("Set the location of item pin to \(String(describing: dropPin.coordinate))")

    }

    // Styles
    itemInfoView.layer.cornerRadius = 3
    itemInfoView.layer.shadowColor =  UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor
    itemInfoView.layer.shadowOffset = CGSize(width: 0, height: 1.75)
    itemInfoView.layer.shadowRadius = 1.7
    itemInfoView.layer.shadowOpacity = 0.45

    itemLocationView.layer.cornerRadius = 3
    itemLocationView.layer.shadowColor =  UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor
    itemLocationView.layer.shadowOffset = CGSize(width: 0, height: 1.75)
    itemLocationView.layer.shadowRadius = 1.7
    itemLocationView.layer.shadowOpacity = 0.45

    locationAdressTextview.layer.cornerRadius = 2
    locationAdressTextview.layer.shadowColor =  UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor
    locationAdressTextview.layer.shadowOffset = CGSize(width: 0, height: 1.50)
    locationAdressTextview.layer.shadowRadius = 1.6
    locationAdressTextview.layer.shadowOpacity = 0.3
}

我知道应用 将引脚坐标保存为CGPoint,我知道它会将其从CGPoint转换为CLLocationCoordinate2D,因为我提出的print陈述。但是,当屏幕加载时,print语句显示一个有效的坐标,但地图上没有引脚,我没有错误。有人能帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:1)

每当地图显示注释时,都会调用MKMapViewDelegate方法viewForAnnotation,因此您必须实施该方法并根据需要返回视图

示例代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView")
    // configure the view

    return annotationView
}