How to set custom pin image in Mapview swift ios?
Asked Answered
S

2

5
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
    map.delegate = self
    showAlarms()
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func showAlarms(){

    map.region.center.latitude = 10.733051
    map.region.center.longitude = 76.763042
    map.region.span.latitudeDelta = 1
    map.region.span.longitudeDelta = 1


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

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        //pinView!.animatesDrop = true
        pinView!.image = UIImage(named:"annotation")!

    }
    else {
        pinView!.annotation = annotation
    }

    return pinView
}

I need to show to a custom pin image for my mapview in swift ios. The custom image is not loading neither the pin. I have an image named "annotation" which I need to load in my mapview.

Sachsen answered 23/1, 2017 at 6:20 Comment(8)
The delegate method of viewForAnnotation is Called for You or not.Vasti
@KiritModi. I don't thinks so. It's showing only the map and nothing else. Please help.Sachsen
You are code in swift 3Vasti
@KiritModi I am using swift3 but the code is not working.Sachsen
Added new code of showAlarms.Vasti
@KiritModi. I have added the code. But it's still not working. But when I checked the answer given by Lalit Kumar it is working. But when the mapview loads full map is showing. I need to show only the area near my cordinates. How can I achieve that?Sachsen
Let us continue this discussion in chat.Sachsen
Use MKAnnotationView instead of MKPinAnnotationView to have custom pin image.Device
P
7
  mapview.delegate = self

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
    return nil
}

 let annotationIdentifier = "Identifier"
 var annotationView: MKAnnotationView?
 if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
    annotationView = dequeuedAnnotationView
    annotationView?.annotation = annotation
}
else {
    annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}

 if let annotationView = annotationView {

    annotationView.canShowCallout = true
    annotationView.image = UIImage(named: "yourImagename”)
}
  return annotationView
}
Passant answered 23/1, 2017 at 6:30 Comment(0)
S
1

You can also create your custom MKAnnotationView and set the image inside:

class myCustomAnnotationView : MKAnnotationView {

    override var annotation: MKAnnotation? {
    willSet {
        guard let annotation = newValue else {return}
        switch annotation {
        case is myCustomMKAnnotation:
            self.canShowCallout = true 
            self.image = #yourimage
            self.centerOffset = CGPoint(x: 0, y: -self.image!.size.height / 2)
            break
        default:
            return
        }
        self.setAnimationLayer()
    }
}

And then all you need to do it:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { return nil }
    guard !(annotation is MKUserLocation) else {


    let annotationIdentifier = "Identifier"
    if let myAnnotation = annotation as? myCustomMKAnnotation {
        let annotation = myCustomAnnotationView(annotation: siteAnnotation, reuseIdentifier: annotationIdentifier)
        return annotation
    }
    return nil
}

I think it looks cleaner like that

Saddletree answered 13/12, 2017 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.