0

I'm trying to show an image annotation instead of a pin annotation on the mapkit. I'm using this code:

import UIKit
import MapKit

class ViewController2: UIViewController , MKMapViewDelegate  {

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self

        let coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)

        let region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        mapView.setRegion(region, animated: true)



        var info1 = CustomPointAnnotation()
        info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)
        info1.title = "Info1"
        info1.subtitle = "Subtitle"
        info1.imageName = "taxi"

        var info2 = CustomPointAnnotation()
        info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)
        info2.title = "Info2"
        info2.subtitle = "Subtitle"
        info2.imageName = "smile"

        mapView.addAnnotation(info1)
        mapView.addAnnotation(info2)

        mapView.showAnnotations(mapView.annotations, animated: true)
    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        print("delegate called")

        if !(annotation is CustomPointAnnotation) {
            return nil
        }

        let reuseId = "test"

        var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            anView?.canShowCallout = true
        }
        else {
            anView?.annotation = annotation
        }

        //Set annotation-specific properties **AFTER**
        //the view is dequeued or created...

        let cpa = annotation as! CustomPointAnnotation
        anView?.image = UIImage(named:cpa.imageName)

        return anView
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(_ animated: Bool) {

    }


}

class CustomPointAnnotation: MKPointAnnotation {
    var imageName: String!
}

I think this method is not calling viewForAnnotation, although I connect the delegate with viewcontroller : mapView.delegate = self.

I still see the pin annotation instead of custominnotation on the map:

Map

fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
  • Possible duplicate of [iOS Swift MapKit Custom Annotation](http://stackoverflow.com/questions/38274115/ios-swift-mapkit-custom-annotation) –  Feb 04 '17 at 20:36
  • If you are familiar with setting break points and tracing your code, see when the code comes to your viewForAnnotation delegate does it pass the "if !(annotation is CustomPointAnnotation)" or it return's there. – Rhm Akbari Feb 04 '17 at 21:20

1 Answers1

0

now when implement this code :

import UIKit
import MapKit

class ViewController2: UIViewController , MKMapViewDelegate  {

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self

        let coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)

        let region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        mapView.setRegion(region, animated: true)



        let annotation = MKPointAnnotation()
        annotation.title = "Annotation Created"
        annotation.subtitle = "mahdi"
        annotation.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)

        let annotation2 = MKPointAnnotation()
        annotation2.title = "Annotation Created"
        annotation2.subtitle = "mahdi"
        annotation2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)



        mapView.addAnnotation(annotation)
        mapView.addAnnotation(annotation2)

    }


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

        guard !annotation.isKind(of: MKUserLocation.self) else {

            return nil
        }

        let annotationIdentifier = "AnnotationIdentifier"

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            annotationView!.canShowCallout = true
        }
        else {
            annotationView!.annotation = annotation
        }

        annotationView!.image = UIImage(named: "taxi")

        return annotationView

    }

i can see the two images annotations like this :

enter image description here

now i want to include this image with user location , i try with this code but still see the red pin annotation instead of image

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


        if let location = manager.location?.coordinate {

            userLocation  = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)

           // print(userLocation)

            if driverOnTheWay == false {

                let region = MKCoordinateRegion(center: userLocation, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
                self.mapView.setRegion(region, animated: true)
                self.mapView.removeAnnotations(self.mapView.annotations)


                let annotation = MKPointAnnotation()
                annotation.title = "مكانك هنا"
                annotation.subtitle = "mahdi"
                annotation.coordinate = userLocation
                self.mapView.addAnnotation(annotation)




            }

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

                guard !annotation.isKind(of: MKUserLocation.self) else {

                    return nil
                }

                let annotationIdentifier = "AnnotationIdentifier"

                var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)

                if annotationView == nil {
                    annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
                    annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
                    annotationView!.canShowCallout = true
                }
                else {
                    annotationView!.annotation = annotation
                }

                annotationView!.image = UIImage(named: "car")

                return annotationView

            }

how can show car image with user location