Learn Courses My Dashboard

Default Pin Still appears with UIImage

Hello,

My UIImage pin shows up with the default pin. How can I just show my UIImage? Here is the exact code example:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate {
    
    let coordinate =  CLLocationCoordinate2D(
        latitude: 40.728,
        longitude: -74
    )

    let map = MKMapView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.addSubview(map)
        map.frame = view.bounds
        
        map.setRegion(MKCoordinateRegion(
            center: coordinate,
            span: MKCoordinateSpan(
                latitudeDelta: 0.1,
                longitudeDelta: 0.1
            )
        ),
        animated: false)
        
        map.delegate = self
        
        addCustomPin()
    }

    private func addCustomPin(){
        let pin = MKPointAnnotation()
        pin.coordinate = coordinate
        pin.title = "Pokemon Here"
        pin.subtitle = "Go and catch them all!"
        map.addAnnotation(pin)
    }
    
    // Map
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }
        var annotationView = map.dequeueReusableAnnotationView(withIdentifier: "custom")
        
        if annotationView == nil {
            // Create the view
            annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "custom")
            
            annotationView?.canShowCallout = true
        }
        else{
            annotationView?.annotation = annotation
            
        }
        
        annotationView?.image = UIImage(named: "ball")
        
        
        return annotationView
    }
    
}


This stack overflow thread might assist:

I had implemented the code from the link you provided, but I still get a UIImage with the red pin. I need to get rid of the red pin. It gets in the way :

I take that back! It worked great!