How to add iOS 10 default annotation/pin?
Asked Answered
H

2

6

I have a MapView in my app. And user can add an annotaion/pin to that MapView. I works fine but I would like to know if it is possible to add new annotation design.

This is the new pin:

enter image description here

This is what my map shows:

enter image description here

This is my annotation code:

let annotaion = MKPointAnnotation()

this line adds my annotation to map:

self.myMap.addAnnotation(self.annotaion)

Thank you.

Hudak answered 21/10, 2016 at 15:8 Comment(2)
Is your deployment target is iOS 10?Chivalrous
Yep. But I started making this app in iOS 9Hudak
T
2

I believe @0ndre_ was looking for the default image, not to bring in his own custom image. The answer by @Franco_Meloni was incomplete. Here is how I got it to work.

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

        let pin = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil)
        pin.canShowCallout = true

        let button = UIButton(type: .detailDisclosure)
        pin.rightCalloutAccessoryView = button

        if annotation.isEqual(mapView.userLocation) {
            return nil
        }
        return pin
    }

The key is using MKMarkerAnnotationView to get the bigger marker. If you use MKPinAnnotationView you will see the smaller one appear.

Traject answered 1/4, 2019 at 14:47 Comment(0)
D
1

You should implement

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

from MKMapViewDelegate (https://developer.apple.com/reference/mapkit/mkmapviewdelegate/1452045-mapview)

Implementation example:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    let view = MKAnnotationView()
    view.image = UIImage(named: "pin")

    return view
}
Duala answered 21/10, 2016 at 15:22 Comment(5)
When I use this: func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let view = MKAnnotationView() view.image = UIImage(named: "pin") return view } it returns an empty map.. :/Hudak
is your image resource named "pin"?Duala
No I Have not :/ . I do not know where is my image resourceHudak
If is in your bundle use let path = self.bundle.pathForResource("imageName", ofType: "jpg")Duala
You have to create the image yourself in this caseHume

© 2022 - 2024 — McMap. All rights reserved.