0

Here is my code. I have tried all the solutions available but I am not able to put image I wish to instead of the default annotation red pin in my code.

import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet var map: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    var location = CLLocationCoordinate2DMake(18.956449, 72.810597)

    var span = MKCoordinateSpanMake(0.009, 0.009)
    var region = MKCoordinateRegion(center: location, span: span)

    map.setRegion(region, animated: true)

    var annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = "Wilson College"
    annotation.subtitle = "Commerce College"

    map.addAnnotation(annotation)
    print("showing on map")

    mapView(map, viewForAnnotation: annotation)


}



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

    let identifier = "MyPin"
    print("inside viewForAnnotation")
    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)

    // Reuse the annotation if possible
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

    if annotationView == nil
    {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView!.canShowCallout = true
        annotationView!.image = UIImage(named: "VRtest.png")
        annotationView!.rightCalloutAccessoryView = detailButton
        print("editing annotation")
    }
    else
    {
        annotationView!.annotation = annotation
    }

    return annotationView
}

enter image description here

Any help is appreciated. thank you

user4261201
  • 2,324
  • 19
  • 26
  • **NEVER** call `viewForAnnotation` by yourself. The delegate method is called by the framework. – vadian Apr 30 '16 at 15:01
  • plz guide, I am an amateur. I have stated some print statements to understand the flow of the code. using the current code, I get these statements in the output box, "showing on map," "inside viewForAnnotation," "editing annotation," and if I remove the line - mapView(map, viewForAnnotation: annotation), then I get this statement - "showing on map" only. – SagarWadekar May 02 '16 at 07:13

1 Answers1

0

What do you want is to customize the default annotation? if YES, you can refer this post How to create Custom MKAnnotationView and custom annotation title and subtitle

Here is the example in Swift 2

class MapViewController: UIViewController {


    @IBOutlet weak var mapView: MKMapView!

    let initialLocation = CLLocation(latitude: 34.074094, longitude: -118.396329)
    let regionRadius: CLLocationDistance = 1000

    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
            regionRadius * 2.0, regionRadius * 2.0)
        self.mapView.setRegion(coordinateRegion, animated: true)
    }

    override func viewDidLoad() {
        self.mapView.delegate = self
        self.centerMapOnLocation(initialLocation)

        let location = CLLocationCoordinate2D(latitude: 34.074094, longitude: -118.396329)
        let annotation = CustomAnnotation(coordinate: location, imageName: "pinIcon", title: "Test", subtitle: "Detail", enableInfoButton: false)

        self.mapView.addAnnotation(annotation);
    }
}

extension MapViewController: MKMapViewDelegate {
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        if (annotation is MKUserLocation) {
            return nil
        }

        let pinIdentifier = "pinIdentifier"
        var view: MKAnnotationView
        if (annotation.isKindOfClass(CustomAnnotation)) {
            if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinIdentifier)
                as MKAnnotationView? {
                    dequeuedView.annotation = annotation
                    view = dequeuedView
            } else {
                view = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdentifier)
                view.canShowCallout = true

                //Set custome image
                let customAnnotation = annotation as! CustomAnnotation
                view.image = UIImage(named:customAnnotation.imageName!)
            }

            return view
        }

        return nil
    }

}

class CustomAnnotation : NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D
    var imageName: String?
    var title: String?
    var subtitle: String?
    var enableInfoButton : Bool

    init(coordinate: CLLocationCoordinate2D, imageName: String, title: String, subtitle: String, enableInfoButton : Bool) {
        self.coordinate = coordinate
        self.imageName = imageName
        self.title = title
        self.subtitle = subtitle
        self.enableInfoButton = enableInfoButton;
    }
}

This is my custom image displaying on the screen

enter image description here

Community
  • 1
  • 1
HSG
  • 1,224
  • 11
  • 17
  • grateful for your help. the answer gives code for objectiveC, is there any code for swift2. please share link for swift2 code if possible. if there is a working sample project, that would be of great help. – SagarWadekar May 02 '16 at 07:08
  • It worked. HDT, you are amazing. thank you for helping. – SagarWadekar May 03 '16 at 05:03
  • Just one more thing in this, can we have drop effect for the annotation? what do we edit in this code? – SagarWadekar May 03 '16 at 05:12
  • One observation: the image that I have put using this code is not to the exact point (spot) as compared the default red pin of swift code, only when I zoom-in, then it shows exact location. anything can be done in that case..... ALso, one more addition in this, can we have drop effect for the annotation? what do we edit in this code? – SagarWadekar May 03 '16 at 05:20