Oscar
Oscar

Reputation: 541

Hide map pin annotation image

I am creating a map application which allows emojis to be attached to locations. Currently, the image is displayed above the stock apple map annotation.

I wish to hide the stock pin image while still displaying the selected emoji by the user. Is this possible?

enter image description here

I have this code so far to perform this:

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

    //let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let dictName = arrLocation[((annotation as? MyAnnotation)?.index)!]
    let imgName = dictName.valueForKey("pin_img") as? String
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(imgName!)
    if annotationView == nil
    {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: imgName)
        annotationView!.canShowCallout = false
        let name = dictName.valueForKey("name") as! String
        let annot = MyAnnotation(titleName:name)
        annotationView?.annotation = annot

    }
    else
    {
        annotationView!.annotation = annotation
    }

    let detailButton: UIButton = UIButton(type: UIButtonType.Custom)
    // size and placement of emoji on map
    detailButton.frame = CGRectMake(-34,-25,85,85)
    detailButton.tag = ((annotation as? MyAnnotation)?.index)!
    detailButton.addTarget(self, action:"emojiTap:", forControlEvents: UIControlEvents.TouchUpInside)
    detailButton.setBackgroundImage(UIImage(named:(dictName.valueForKey("pin_img") as? String)!), forState: UIControlState.Normal)
    annotationView!.addSubview(detailButton)

    return annotationView
}

Upvotes: 1

Views: 1948

Answers (1)

Luke Van In
Luke Van In

Reputation: 5265

Custom annotation view

Instead of using an MKPinAnnotationView, use its superclass MKAnnotationView.

Responding to input

To respond to the user tapping on an annotation implement the mapView:didSelectAnnotation: delegate method. This delegate method is called when the annotation is tapped.

Note that you do not need to use the button inside the annotation view. This means the functionality in emojiTap: must be implemented by the mapView:didSelectAnnotation: method.

Example

Combining these

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

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let dictName = arrLocation[((annotation as? MyAnnotation)?.index)!]
    let imgName = dictName.valueForKey("pin_img") as? String
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(imgName!)

    if annotationView == nil
    {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: imgName)
        let name = dictName.valueForKey("name") as! String
        let annot = MyAnnotation(titleName:name)
        annotationView?.annotation = annot    
    }
    else
    {
        annotationView!.annotation = annotation
    }

    annotationView!.canShowCallout = false

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    
    guard let annotation = view.annotation as? MyAnnotation else {
        return
    }

    print("tapped annotation: \(annotation.index)

    // Implement the code from emojiTap: here.
}

Upvotes: 1

Related Questions