0

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
    }
Undo
  • 25,519
  • 37
  • 106
  • 129
  • You need to add your image to annotaionView. Please check https://stackoverflow.com/questions/41480737/custom-annotation-swift – Vini App Sep 28 '17 at 00:35

2 Answers2

1

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
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • How would that work with the code I have now? Obviously, copying that in doesn't work because I already have a custom annotation. I don't understand how to merge both bits of code to work. – Doing Things Occasionally Sep 28 '17 at 15:15
  • @ATrueNovice Can I see your code of “custom annotation”? – Kosuke Ogawa Sep 28 '17 at 15:26
  • I just added it to the original question. I have a specific annotation for the user and another one for all the locations. I have the Location Annotation in my ViewDidLoad and the user annotation in the View For Annotation. I tried moving the location annotations but, since I dictionary unwrapping the firebase data in the view did load, It doesn't work any place else. – Doing Things Occasionally Sep 28 '17 at 15:30
1

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()
    }
}
josef
  • 867
  • 12
  • 28
Wang90925
  • 142
  • 6