Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ended up with a completely different strategy. Instead of adding by own overlay I now rely on <code>MKPolyLine</code>.</p> <p>Using the following code I'm able to add a pseudo animated line from point A to point B on a <code>MKMapView</code>. </p> <p>The code adds several overlays to the MKMapViews with a slight delay, giving the impression of an animation</p> <p>Not the most beautiful solution! - but it looks pretty good in action :-)</p> <pre><code>/*start the animation*/ -(void)plotRouteOnMap { [self.mapView removeOverlays:self.mapView.overlays]; //calculate a number locations between the two locations self.points = [self getPointsOnRouteFrom:&lt;FROM_LOCATION&gt; to:&lt;TO_LOCATION&gt; onMapView:self.mapView]; [self addOverlaysFromPointsWithStartFrom:[NSNumber numberWithInt:1]]; } /*convert a CGPoint to a CLLocation according to a mapView*/ - (CLLocation*)pointToLocation:(MKMapView *)mapView fromPoint:(CGPoint)fromPoint { CLLocationCoordinate2D coord = [mapView convertPoint:fromPoint toCoordinateFromView:mapView]; return [[[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude] autorelease]; } /*get a list of Location objects between from and to*/ -(NSArray*)getPointsOnRouteFrom:(CLLocation*)from to:(CLLocation*)to onMapView:(MKMapView*)mapView { int NUMBER_OF_PIXELS_TO_SKIP =10; //lower number will give a more smooth animation, but will result in more layers NSMutableArray *ret = [NSMutableArray array]; CGPoint fromPoint = [mapView convertCoordinate:from.coordinate toPointToView:mapView]; CGPoint toPoint = [mapView convertCoordinate:to.coordinate toPointToView:mapView]; NSArray *allPixels = [self getAllPointsFromPoint:fromPoint toPoint:toPoint]; for (int i = 0 ; i &lt; [allPixels count] ; i+=NUMBER_OF_PIXELS_TO_SKIP) { NSValue *pointVal = [allPixels objectAtIndex:i]; [ret addObject:[self pointToLocation:mapView fromPoint:[pointVal CGPointValue]]]; } [ret addObject:[self pointToLocation:mapView fromPoint:toPoint]]; return ret; } /*calulate alle pixels from point to toPint*/ -(NSArray*)getAllPointsFromPoint:(CGPoint)fPoint toPoint:(CGPoint)tPoint { /*Simplyfied implementation of Bresenham's line algoritme */ NSMutableArray *ret = [NSMutableArray array]; float deltaX = fabsf(tPoint.x - fPoint.x); float deltaY = fabsf(tPoint.y - fPoint.y); float x = fPoint.x; float y = fPoint.y; float err = deltaX-deltaY; float sx = -0.5; float sy = -0.5; if(fPoint.x&lt;tPoint.x) sx = 0.5; if(fPoint.y&lt;tPoint.y) sy = 0.5; do { [ret addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]]; float e = 2*err; if(e &gt; -deltaY) { err -=deltaY; x +=sx; } if(e &lt; deltaX) { err +=deltaX; y+=sy; } } while (round(x) != round(tPoint.x) &amp;&amp; round(y) != round(tPoint.y)); [ret addObject:[NSValue valueWithCGPoint:tPoint]];//add final point return ret; } /*add a poly line overlay to mapview which start at position 0 and end in 'end' in the array points*/ -(void)addOverlaysFromPointsWithStartFrom:(NSNumber*)end { int intEnd = [end intValue]; //construct polyline view from start CLLocationCoordinate2D *locations = malloc(sizeof(CLLocationCoordinate2D)*2); CLLocation *loc1 = (CLLocation*)[points objectAtIndex:0]; CLLocation *loc2= (CLLocation*)[points objectAtIndex:intEnd]; locations[0] = loc1.coordinate; locations[1] = loc2.coordinate; MKPolyline *line = [MKPolyline polylineWithCoordinates:locations count:2]; [self.mapView addOverlay:line]; if((intEnd+1) &lt; [points count])//add more overlays after delays unless this is the endpoint { [self performSelector:@selector(addOverlaysFromPointsWithStartFrom:) withObject:[NSNumber numberWithInt:intEnd + 1] afterDelay:0.01]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload