Search code examples
swiftmapkitmapkitannotation

MapKit addAnnotation vs addAnnotations


I'm building an application where I'll have several annotations added to a map. As a first test, I tried

let anotacao = MKPointAnnotation()
anotacao.coordinate.latitude = -23.623558
anotacao.coordinate.longitude = -46.581787
anotacao.title = "Annotation 1 Title"
anotacao.subtitle = "Annotation 1 Subtitle"
self.mapa.addAnnotation(anotacao)

anotacao.coordinate.latitude = -23.623658
anotacao.coordinate.longitude = -46.582787
anotacao.title = "Annotation 2 Title"
anotacao.subtitle = "Annotation 2 Subtitle"
self.mapa.addAnnotation(anotacao)

anotacao.coordinate.latitude = -23.623258
anotacao.coordinate.longitude = -46.591787
anotacao.title = "Annotation 3 Title"
anotacao.subtitle = "Annotation 3 Subtitle"
self.mapa.addAnnotation(anotacao)

My expected result was to see the 3 annotations on the map but I had just the last one. I also tried adding those line of codes to

Timer.scheduledTimer(withTimeInterval: 8, repeats: true) { (timer) in
         if index == 1 {
                 anotacao.coordinate.latitude = -23.623558
                 anotacao.coordinate.longitude = -46.581787
                 anotacao.title = "Annotation 1 Title"
                 anotacao.subtitle = "Annotation 1 Subtitle"
                 self.mapa.addAnnotation(anotacao)
                 index += 1
         } else if index == 2 {
                 anotacao.coordinate.latitude = -23.623658
                 anotacao.coordinate.longitude = -46.582787
                 anotacao.title = "Annotation 2 Title"
                 anotacao.subtitle = "Annotation 2 Subtitle"
                 self.mapa.addAnnotation(anotacao)
                 index += 1
         } else if index == 3 {
                 anotacao.coordinate.latitude = -23.623258
                 anotacao.coordinate.longitude = -46.591787
                 anotacao.title = "Annotation 3 Title"
                 anotacao.subtitle = "Annotation 3 Subtitle"
                 self.mapa.addAnnotation(anotacao)
        }

Even though not "usable" in a real project, code above showed 3 annotation on the map.

Searching Stackoverflow I found another way to do it that is using addAnnotations, which is doing exactly what I needed. MapKit in Swift, Part 3 - MKPointAnnotation, addAnnotations My final code looks exactly as suggested in that thread.

As I am very new to Swift programming my main point with this post is try to understand why the first code hadn't worked. What am I missing with addAnnotation?

I'm using Xcode 11.2, Swift4, IOS13 and macOS Catalina


Solution

  • The bit you're missing is that MKPointAnnotation is a reference type (a class) and that the map stores a reference to the instance of the class that you create. Therefore each time you attempt to add a new annotation you are actually updating the properties of the existing one, rather than creating and adding a new one. That's why it was always the last set of values that were visible.

    To make your first example work you'd need to create a new MKMapAnnotation for each of the three annotations.

    (I'm actually surprised the timer-based one works, as you still appear to updating the same object. I'll have a play with that one...)