Post

Replies

Boosts

Views

Created

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.
1
0
741
May ’21