1
votes

I am working on a map application in which i have to animate movement of an aeroplane, based on user location

So i implemented a custom class MKPlaneAndArrow by subclassing MKOverlay

class MKPlaneAndArrow : NSObject, MKOverlay {

  //@objc dynamic var coordinate: CLLocationCoordinate2D
  //the above method also does not change location

  //to change the coordinate after initialization
  var coordinate: CLLocationCoordinate2D {
    willSet {
      willChangeValue(forKey: #keyPath(coordinate))
    }
    didSet {
      didChangeValue(forKey: #keyPath(coordinate))
    }
  }

  var boundingMapRect: MKMapRect

  init( center: CLLocationCoordinate2D, boundingMapRect_: MKMapRect) {

    self.coordinate = center
    self.boundingMapRect = boundingMapRect_

  }

}

Now, when location delegate didUpdateLocations is called, i want to change the coordinate of MKPlaneAndArrow overlay to the current location coordinate so i wrote the following inside didUpdateLocations

    //if not already added annotation
if !HomeVC.flagAlreadyAddedPlaneAnnotation {

    let planeAndArrow = MKPlaneAndArrow.init(center: lastUserLocation.coordinate, boundingMapRect_: mapRect)
    mapView.add(planeAndArrow)
    //Did updateLocations will not add the annotation again
    HomeVC.flagAlreadyAddedPlaneAnnotation = true

}
//get last user location
guard let lastUserLocation:CLLocation = locations.last  else {
    print("no location")
    return
}
 //change coordinate of `MKPlaneAndArrow` overlay
for overlay in mapView.overlays {

    if let myPlane = overlay as? MKPlaneAndArrow {
     myPlane.coordinate = lastUserLocation.coordinate 
    }
}

I have setup breakpoints and they hit the line myPlane.coordinate = ... But i don't know why on earth the coordinate change is not visible on map


Animating

If the above code changes the coordinate, it will not animate the change. I also want it to animate. Like movement of user location on Google Maps or apple maps

I was successfully able to change the coordinate of MKAnnotation with animation. But with overlay, i have been searching for hours but could not find a solution

I saw the following questions on SO along with many others

Notes:

  • Can't Use MkAnnotation because these stay the same size if you zoom, while overlay sizes are relative to the zoom scale

  • If you need my MKOverlayRenderer class, just ask in the comments. My renderer is just drawing an image using `context.draw

Thanks in advance

1
The problem is that the only thing you can animate is a view (or layer), and you don't have one of those.matt
Thanks. So @matt. what will be the best way to handle this problem?Awais Fayyaz
Well, I really don't think your airplane was ever appropriate as an overlay to begin with. An overlay is a drawing delineating some region of the surface of the earth. An annotation is a drawing associated with a coordinate. I think you'd be happier with an annotation whose drawing size changes together with the zoom of the map, as discussed here for example: stackoverflow.com/questions/8286711/…matt

1 Answers

0
votes

I resorted in calculating "UIKit" coords from CLLocationsCoords, adding a std UIImageView so I move animate.. poor solution but very efficient. :(