16

I am trying to create a polyline (MKPolyline) overlay that updates periodically, to simulate the movement of an object. I can achieve this by removing the old overlay, updating the polyline and adding the overlay again, but this leads to flickering.

For a point annotation (MKPointAnnotation) you can simply change its coordinate, and the view will be updated automatically and smoothly without having to remove and re-add the annotation.

Is this also possible somehow for an overlay?

adriaan
  • 1,574
  • 14
  • 33
  • I have posted a full implementation [here][1] [1]: http://stackoverflow.com/questions/11282271/draw-line-on-mkmapview-with-pattern-image/11344230#11344230 – EsbenB Jul 05 '12 at 12:25

3 Answers3

15

yea, you would have to add an additional overlay with the point set of from your last point to your next point. Once you create the MKPolyline with your points, your not able to change it when it draws the MKPolylineView without removing the old and adding the newly created one.

you could create a new polyline view with all the points (including the new one) and add it to the map but do not remove the older one. then once the new one is added, you can remove the older shorter one. It might not be pretty to implement but it should get rid of the flashing on updates. you can distinguish the old and the new with a tag. maybe a point count as the tag would work.

AtomRiot
  • 1,869
  • 3
  • 18
  • 24
  • thanks for your answer. I've implemented it the way you suggest and that's working pretty nicely. As you say, not the prettiest implementation, but probably the best I can do without writing my own PolyLine classes. – adriaan Jul 11 '10 at 00:16
  • Thanks for the answer. Unfortunately this probably does not solve my problem, since my overlay is semi-transparent. I would probably have to add my own view on top of the map. – huggie Apr 07 '12 at 06:24
12

All of the MapKit overlays are immutable so in order to get mutability you need to build your custom overlay and redraw only the region that needs to be updated.

You can find an example in the Breadcrumb sample application example from Apple. Breadcrumb link

Sorin Antohi
  • 6,145
  • 9
  • 45
  • 71
  • Thanks @SorinA - As I mentioned I went with @AtomRiot's approach at the time. But in the next update for the app I'll check out the Breadcrumb sample to see if that can be an improvement. – adriaan Mar 08 '11 at 05:01
1

The MKPolyline class inherits MKMultiPoint, which consists of a set of points. This is a property that is read-only, meaning, unfortunately, you can't update it.

Peter Zich
  • 512
  • 3
  • 13