George B George B - 17 days ago 5
Swift Question

My annonation isn't visible when I add it in the viewDidLoad of my app

I am developing an app where the user has to mark a location on a map, and then save it. My app then archives it, and when the user wants to see that location, the app is supposed to retreive the location's coordinates, and add an annonation on the map if the coordinates exist. Here is the piece of code I created to display the information the user selected-

@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)

}


All the print statements I've created work, so I know that the app does save the coordinates, and that it does convert them properly. However, when the app loads the information in the
if let item = item
portion of the
viewDidLoad
, the pin isn't dropped onto the map like it's supposed to.

Does anybody see why my code isn't working the way it's supposed to? Thanks!

If you need my full code, tell me, and I will put it into my question.

Answer

You need to use

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

        return annotationView
}

outside your viewDidLoad() function