Xcode Swift - How to ask the user display their pin from a UI button

I'm new Xcode swift and I'm trying to let the user press a button to display the pin (custom annotation in this case) in the map but I'm not sure how to do it.

Here's the code so far:

import CoreLocation

import MapKit

import UIKit



class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    

    @IBOutlet var mapView: MKMapView!

    

    let manager = CLLocationManager()



    override func viewDidLoad() {

        super.viewDidLoad()

        

    }



    override func viewDidAppear( animated: Bool) {

        super.viewDidAppear(animated)

        manager.desiredAccuracy  = kCLLocationAccuracyBest

        manager.delegate = self

        manager.requestWhenInUseAuthorization()

        manager.startUpdatingLocation()

    }

    

    func locationManager(
manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

        if let location = locations.first{

            manager.stopUpdatingLocation()

            

            render(location: location)

        }

    }

    

    func render(location: CLLocation){

        

        let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

        

        let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)

        

        let region = MKCoordinateRegion(center: coordinate, span: span)

        

        mapView.setRegion(region, animated: true)

        

        let pin = MKPointAnnotation()

        pin.coordinate = coordinate

        pin.title = "COVID-19 Case"

        pin.subtitle = "There is COVID-19 cases in your area. "

        mapView.addAnnotation(pin)

        

        

    }

    



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

        guard !(annotation is MKUserLocation) else{

            return nil

        }

        

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "custom")

        

        if annotationView == nil {

            //Create the view

            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "custom")

            

            annotationView?.canShowCallout = true

        }

        else{

            annotationView?.annotation = annotation

        }

        

        annotationView?.image = UIImage(named: "viruscase")

        

        return annotationView

        

    }



    

}

Any help would be appreciated.
I noted some errors in the following:
you need to add the underscore before manager
Code Block
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

The same for viewDidAppear:
Code Block
    override func viewDidAppear( _ animated: Bool) {


I edited your code to format it without all the extra empty lines and used the code formatter (<>):
Code Block
import CoreLocation
import MapKit
import UIKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear( _ animated: Bool) {
super.viewDidAppear(animated)
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.delegate = self
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
if let location = locations.first{
manager.stopUpdatingLocation()
render(location: location)
}
}
func render(location: CLLocation){
let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegion(center: coordinate, span: span)
mapView.setRegion(region, animated: true)
let pin = MKPointAnnotation()
pin.coordinate = coordinate
pin.title = "COVID-19 Case"
pin.subtitle = "There is COVID-19 cases in your area. "
mapView.addAnnotation(pin)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "custom")
if annotationView == nil {
//Create the view
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "custom")
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
annotationView?.image = UIImage(named: "viruscase")
return annotationView
}
}


To show / hide annotations, create an IBAction for the button and remove or add annotations.
Create a var to keep status (are annotations visible).

Here is an example from https://stackoverflow.com/questions/31266837/show-hide-annotation-when-button-is-pressed
You'll have to adapt to your annotation set: only one annotation ?
The simplest would be to keep annotation parameters in a property of the class and use it in addAttractionPins
Code Block
var annotationIsVisible = false
@IBAction func showAnnotation(sender: AnyObject) {
if !annotationIsVisible {
addAttractionPins()
annotationIsVisible = true
}else {
Map.removeAnnotations(Map.annotations)
annotationIsVisible = false
}
}
func addAttractionPins() {
let filePath = NSBundle.mainBundle().pathForResource("Attractions", ofType: "plist")
let attractions = NSArray(contentsOfFile: filePath!)
for attraction in attractions! {
let point = CGPointFromString(attraction["location"] as! String)
let coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(point.x), CLLocationDegrees(point.y))
let title = attraction["name"] as! String
let typeRawValue = (attraction["type"] as! String).toInt()!
let type = AttractionType(rawValue: typeRawValue)!
let subtitle = attraction["subtitle"] as! String
let annotation = AttractionAnnotation(coordinate: coordinate, title: title, subtitle: subtitle, type: type)
mapView.addAnnotation(annotation)
}
}


Xcode Swift - How to ask the user display their pin from a UI button
 
 
Q