当我在我的应用程序的viewDidLoad中添加它时,我的annonation不可见

时间:2016-11-21 18:08:40

标签: ios swift coordinates archive cgpoint

我正在开发一个应用,其中用户必须在地图上标记位置,然后保存它。我的应用程序然后将其归档,当用户想要查看该位置时,应用程序应该检索位置的坐标,并在坐标存在时在地图上添加一个annonation。以下是我创建的用于显示用户选择的信息的代码 -

@IBOutlet var mapGestureRecognizer: UIGestureRecognizer!
var item: itemData?

// Item Outlets
@IBOutlet weak var itemInfoView: UIView!
@IBOutlet weak var itemNameTextField: UITextField!
@IBOutlet weak var itemDescriptionLabel: UITextView!
@IBOutlet weak var mapView: MKMapView!

// Item Location Outlets
@IBOutlet weak var itemLocationView: UIView!
@IBOutlet weak var itemLocationTextView: UITextView!

let dropPin = MKPointAnnotation()

override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView.delegate = self

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

    // Press recognizer

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

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

        return annotationView
    }

    if let item = item {

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

        dropPin.coordinate = mapView.convert(item.mapPoint, toCoordinateFrom: mapView)
        dropPin.title = "Location of \(item.itemName)"
        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

    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
let annotation = MKPointAnnotation()
//let point = mapView.convert(coordinate, toPointTo: overlayView)
func action(gestureRecognizer:UIGestureRecognizer){

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

    else {
        let itemName = itemNameTextField.text
        let touchPoint = gestureRecognizer.location(in: mapView)
        let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView)
        annotation.coordinate = newCoordinates
        annotation.title = "Location of \(itemName!)"
        mapView.addAnnotation(annotation)
    }

}

@IBAction func mapLocationPress(_ sender: UILongPressGestureRecognizer) {
        action(gestureRecognizer: mapGestureRecognizer)

}

我创建的所有打印语句都有效,所以我知道应用程序 保存坐标, 正确转换它们。但是,当应用加载if let item = item viewDidLoad部分中的信息时,引脚不会像预期的那样掉到地图上。

有人看到为什么我的代码没有按照预期的方式工作吗?谢谢!

如果您需要我的完整代码,请告诉我,我会将其置于我的问题中。

2 个答案:

答案 0 :(得分:1)

您需要使用

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView")
        // configure the view

        return annotationView
}

viewDidLoad()函数

之外

答案 1 :(得分:0)

您应该mapViewFunc viewDidLoadviewDidLoad

func viewDidLoad() {
    super.viewDidLoad()
    DispatchQueue.main.async {
        // add pin
    }
}