Custom Annotation Using MKPointAnnotation

This what I'm trying to do:

Use a single custom annotation for all the locations that will populate the map. I have the custom image saved in the assets but, can't make it work.

My code is as follows:

ViewDidLoad()

    if let locationDict = snap.value as? Dictionary<String, AnyObject> {

        let lat = locationDict["LATITUDE"] as! CLLocationDegrees
        let long = locationDict["LONGITUDE"] as! CLLocationDegrees
        let title = locationDict["NAME"] as! String
        let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
        _ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.10, longitudeDelta: 0.10))

        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
        annotation.title = title.capitalized // if you need title, add this line
        self.mapView.addAnnotation(annotation)
    }

Use a single custom annotation for all the locations that will populate the map. I have the custom image saved in the assets but, can't make it work (Details in the viewDidLoad)

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

        var annotationView: MKAnnotationView?


        if annotation.isKind(of: MKUserLocation.self) {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
            annotationView?.image = UIImage(named: "icon")
        }



        return annotationView
    }

Upvotes: 0

Views: 1510

Answers (2)

Wang90925
Wang90925

Reputation: 142

You should implement delegate method like this.

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

        if annotation is MKUserLocation {
            return nil
        }

        var pinView : MKAnnotationView? = nil
        let identifer = "pin"

        pinView = mapView .dequeueReusableAnnotationView(withIdentifier: identifer)
        if pinView == nil {
            pinView = MKAnnotationView.init(annotation: annotation, reuseIdentifier: identifer)
        }

        pinView?.canShowCallout = true
        pinView?.calloutOffset = CGPoint(x: -5, y: 5)
        pinView?.rightCalloutAccessoryView = UIButton.init(type: .detailDisclosure) as UIView

        let type = (annotation as! CustomAnnotation).type

        if type == -1 {
            pinView?.image = UIImage(named: "Img_gray_pin")?.resized(toWidth: 20)
        } else if type == 0 {
            pinView?.image = UIImage(named: "Img_gray_pin")?.resized(toWidth: 20)
        } else if type == 1{
            pinView?.image = UIImage(named: "Img_blue_pin")?.resized(toWidth: 20)
        } else if type == 2 {
            pinView?.image = UIImage(named: "Img_orange_pin")?.resized(toWidth: 25)
        } else if type == 3 {
            pinView?.image = UIImage(named: "Img_red_pin")?.resized(toWidth: 30)
        } else {
            pinView?.image = UIImage(named: "Img_gray_pin")?.resized(toWidth: 20)
        }

        return pinView
    }

And then create CustomAnnotation Class

import MapKit
import UIKit

class CustomAnnotation: NSObject, MKAnnotation {

    let title: String?
    let subtitle: String?
    let coordinate: CLLocationCoordinate2D
    let tag     : Int
    let type    : Int

    init(title: String, address: String, coordinate: CLLocationCoordinate2D, tag : Int, type : Int) {
        self.title = title
        self.subtitle = address
        self.coordinate = coordinate
        self.tag = tag
        self.type = type

        super.init()
    }
}

Upvotes: 1

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

You should implement delegate method:

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

    if annotation.isKind(of: MKUserLocation.self) {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
        annotationView.image = UIImage(named: "icon")
        return annotationView
    }

    let reuseId = "Image"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        annotationView?.canShowCallout = true
        annotationView?.image = UIImage(named: "<<image name>>")
    }
    else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

Upvotes: 1

Related Questions