0

I have an annotations, which is presented on map view. I'm getting the coordinates from API and passing it to custom annotation class. Here is the code:

func pinsOnMap() {
        for pins in flightData {
            dynamic var pinCoordinates = CLLocationCoordinate2D(latitude: pins.geography.latitude!, longitude: pins.geography.longitude!)
            let departure = pins.departure.iataCode
            let arrival = pins.arrival.iataCode
            let regNumber = pins.aircraft.regNumber
            let airline = pins.airline.iataCode
            mapItem = AirplanePin(coordinate: pinCoordinates, image: planeImg!, departure: departure!, arrival: arrival!, regnumber: regNumber!, airline: airline!, flightNumber: pins.flight.iataNumber!, status: pins.status!, courseDegress: 0)
            flightMap.addAnnotation(mapItem)
        }
    }

Here is my custom annotation class:

import UIKit
import MapKit

final class AirplanePin: NSObject, MKAnnotation {
    
    dynamic var coordinate: CLLocationCoordinate2D
    var image: UIImage
    var departure: String?
    var arrival: String?
    var regnumber: String?
    var airline: String?
    var flightNumber: String?
    var status: String?
    var courseDegrees: Double?
    //let itemType: ItemType
    //var image: UIImage { return itemType.image }
    
    init(coordinate: CLLocationCoordinate2D, image: UIImage, departure: String, arrival: String, regnumber: String, airline: String, flightNumber: String, status: String, courseDegress: Double) {
        self.image = image
        self.coordinate = coordinate
        self.departure = departure
        self.arrival = arrival
        self.regnumber = regnumber
        self.airline = airline
        self.flightNumber = flightNumber
        self.status = status
        self.courseDegrees = courseDegress
        //self.itemType = ItemType(rawValue: arc4random_uniform(2)) ?? .green
    }
}

On the main view controller, I've created a timer, which appears every 5 seconds and I put this function to get data from API:

func onFlightsResponse(_ apiManager: FlightApiManager, responseModel: [FlightsModel]) {
        flightData = responseModel
        let myGroup = DispatchGroup()
        
        myGroup.notify(queue: .main) {
            self.pinsOnMap()
            //self.updatePins()
        }
        
    }

So, the main question is: how to make the annotation moving, when getting new coordinates each 5 seconds

I've tried to assign new coordinates, but annotation not moving

1 Answers1

0

This thread says:

In Swift 4.0 and IOS 11.0, I just add dynamic attribute to the coordinate property of child class of MKAnnotation class and it works: All Annotations on MapView update their location if coordinate value of MKAnnotation objects are updated:

Note that the poster says that they change the coordinates of the existing annotations. Your code is adding a new set of annotations each time their coordinates change. Instead you should save your AirplanePin objects in your flightData array, and update them instead of creating new ones each time.

Duncan C
  • 128,072
  • 22
  • 173
  • 272