2

Here's a small problem..

I've done some code which should change the image of the pin that get's dropped in mapView. The url of "taxi.png" works on an image view so that's not the current problem.

I also tried:

pinView?.image = UIImage(named: "taxi.png")

Code:

 if pinView == nil{
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true
        var imageTaxi = UIImage(named: "taxi.png")!
        imageTaxi = imageTaxi.withAlignmentRectInsets(UIEdgeInsetsMake(0, 0, imageTaxi.size.height/2, 0))
        pinView?.image = imageTaxi
        }

        let button = UIButton(type: .detailDisclosure)
        pinView?.rightCalloutAccessoryView = button
        return pinView

Does anybody see what should be fixed here to view the image?

EDIT: I now have this and I still have the standard red pinpoint..

 let annotation = CustomPin()
 annotation.coordinate = location
 annotation.title = "Boek Taxi"
 annotation.subtitle = ""
 let image = UIImage(named: "taxi.png")

 annotation.Image = image
 self.mapView.addAnnotation(annotation)
 let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)

    if pinView == nil{
        pinView = MKPinAnnotationView(annotation: CustomPin() as MKAnnotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true

    } else {
        pinView?.annotation = CustomPin() as MKAnnotation
    }

    let cpa = annotation as! CustomPin

    pinView?.image = cpa.Image

    let button = UIButton(type: .detailDisclosure)
    pinView?.rightCalloutAccessoryView = button
    return pinView
    }

Any things I've over looked?

rvano
  • 97
  • 7
  • Have a look at this [Custom pin image](http://stackoverflow.com/a/38159048/4080925) – MwcsMac Sep 30 '16 at 14:41
  • Confirm that this code is getting called at all (set breakpoint and/or print a log statement). Perhaps the delegate was not specified, or the method signature is not right. If that's not it, them please clarify: when you say there is a "problem", what exactly are you seeing? Red pin? Nothing? Crash? – Rob Sep 30 '16 at 15:40

1 Answers1

-1

Try this

create a class for your custom annotation Example :

class CustomPin: MKPointAnnotation {

   let Image = UIImage

 }

Then in your function for the annotation view do this

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
         let reuseId = "image"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if pinView == nil {

            pinView = MKAnnotationView(annotation: customPin as MKAnnotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true

        } else {

            pinView!.annotation = customPin as MKAnnotation
        }

        let cpa = annotation as! CustomPin
        pinView!.image = cpa.Image

        let button = UIButton(type: UIButtonType.detailDisclosure)

        pinView!.rightCalloutAccessoryView = button


        return anView


        }

Finally add the custom image when you create the annotation itself in your viewDidLoad() or whatever you want like this:

                                let Annotation = customPin()
                                Annotation.coordinate = //cooridnate
                                Annotation.title = //title
                                Annotation.subtitle = //subtitle

                                let image = UIImage(named: "taxi.png")
                                Annotation.Image = image

Hope this helps

Trip Phillips
  • 430
  • 1
  • 5
  • 18
  • @Rob works fine in my app that does exactly what he is asking – Trip Phillips Sep 30 '16 at 16:26
  • Thanks for your Answer @TripPhillips but when I do it like this I still have the red standard pinpoint.. Could you please look at the code which I uploaded in my question post. I would be really thankful – rvano Sep 30 '16 at 22:26
  • @royvano You need to use MKAnnotationView instead of MKAnnotationPinView. That is your issue. So where you define pinView use MKAnnotationView. If you use MKAnnotationPinView you will get a pin every time. – Trip Phillips Sep 30 '16 at 22:37
  • @TripPhilips Thanks! I didn't see that at all, it's working now :) – rvano Oct 02 '16 at 09:34
  • Awesome! Glad I could help! – Trip Phillips Oct 02 '16 at 14:07