1

I'm designing an app using Swift for a food truck. I want to use MapKit to show a pin on the live location of the truck so one can look at the app to see it moving in realtime. I was able to get the server-side element set up using PHP/MySQL. Currently, the app makes a HTTP request every 3 seconds.

How should I go about animating the moving pin (or other image)? I tried a few methods already (if you have others, please suggest!):

  1. I removed the pin and quickly added a new one in my HTTP function. While this works, I would prefer a smooth animation, not a flashing, jerky pin.
  2. I subclassed MKMapViewDelgate like with a didAddAnnotationViews delegate that animates the pin's frame using UIView.animateWithDuration as suggested here. Animation only occurred when the map was initially loaded. Also, I have no idea how this would work with coordinates, since it involves frames.
  3. I subclassed MKAnnotation with a modifiable var coordinate. In my HTTP function, I changed the coordinate. EDIT: This now works since I properly refreshed the mapView (thanks @Paulw11). However, I still have two main issues:

    • There's no linear animation, which I desire to better simulate real-time movement. How would I go about animating the pin's coordinates? Should I use UIView, like in method 2, a fast NSTimer, or something else?
    • Due to using the setCenterCoordinate function to forcibly refresh the map, the map cancels any current touches. How do I detect if there's any touches on the MapView so I can prevent the forced update?
Community
  • 1
  • 1
BradzTech
  • 2,755
  • 1
  • 16
  • 21
  • See http://stackoverflow.com/questions/2256177/how-to-move-a-mkannotation-without-adding-removing-it-from-the-map/5300600#5300600 – Paulw11 Jun 09 '15 at 23:45
  • @Paulw11: Thanks, but I have already tried that (method 1). – BradzTech Jun 09 '15 at 23:48
  • Did you call `viewForAnnotation` after changing the coordinate? There were a couple of other options listed as answers on that question too – Paulw11 Jun 09 '15 at 23:52
  • Oh I see now, thanks! it's working now, but it still isn't a smooth animation like I wanted. – BradzTech Jun 09 '15 at 23:56
  • 1
    Unfortunately if you want smooth animations then I think you will need to interpolate the positions between the start and end locations and animate the intermediate locations – Paulw11 Jun 09 '15 at 23:57
  • have you solved it now. I am working on same thing. – Awais Fayyaz Oct 18 '18 at 13:34
  • @AwaisFayyaz I did, but it was a fairly "hacky" approach of removing and re-creating the pin many times per second. Ended up migrating to Google Maps API. – BradzTech Jan 21 '19 at 16:17

0 Answers0