ios – Update annotation coordinates and make it move in real time



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)

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) {

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