swiftannotationspins

Changing an annotation to a pin


I'm try to make a pin that you click on and a title and subtitle will come up.

Here's my code so far:

    let span : MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
    let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(40.750517, -73.987271)
    let region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    mapView.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()

    annotation.coordinate = location
    annotation.title = "Bus Stop"
    annotation.subtitle = "Street Name at Street Name"
    mapView.addAnnotation(annotation)

The problem is mine comes up as a circle bubble which is not what I want, I want those traditional red pins used in apps that when you click on more info shows.


Solution

  • extension YourViewC: MKMapViewDelegate {

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation
            {
                return nil
            }
            var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "CustomPin")
            if annotationView == nil{
                annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "CustomPin")
                annotationView?.canShowCallout = false
            }else{
                annotationView?.annotation = annotation
            }
            annotationView?.image = UIImage(named: "bluecircle") // Pass name of your custom image
            return annotationView
        }
     func mapView(_ mapView: MKMapView,
                 didSelect view: MKAnnotationView){ 
        let annotation = view.annotation as? CustomAnnotation
       print(annotation?.address) // get info you passed on pin
    
      // write code here to add custom view on tapped annotion
    

    }