28

I have an array of points to be drawn on a map, its already decoded:

- (void) drawRoute:(NSArray *) path {
    NSInteger numberOfSteps = path.count;

    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++) {
         CLLocation *location = [path objectAtIndex:index];
         CLLocationCoordinate2D coordinate = location.coordinate;

         coordinates[index] = coordinate;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [map addOverlay:polyLine];
}

where "map" is an instance of MKMapView, and path the array representing the already decoded set of points.

I thought that with the line [map addOverlay:polyLine]; it would be drawn. I've seen in some pages this method:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 1.0;

    return polylineView;
}

Is the polylineView what is actually drawn on map? I've tried also to pass the MKPolyline (from the method above) to the "<MKOverlay> overlay" argument of this last method, but throws an exception.

I think I'm close, but I don't know what to do now.

Please help! Thank you very much in advance.

David
  • 3,285
  • 1
  • 37
  • 54
Fustigador
  • 6,339
  • 12
  • 59
  • 115

3 Answers3

14

Done.

Was a very stupid thing, i didn't set the delegate for the MapView. Simply adding [map setDelegate:self]; did the trick.

Thank you anyway!.

Fustigador
  • 6,339
  • 12
  • 59
  • 115
4

Just create MKPolyline with coordinates & add that polyLine to map view.

MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [map addOverlay:polyLine];

You will find an tutorial here on how to draw polyline over some coordinates. Edit: The url does not seems valid anymore. You can find the archived version of this url here.

Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39
  • Here a link to an archived version of it: http://web.archive.org/web/20120906022305/http://navarra.ca/?p=786 – Brian Apr 12 '15 at 17:10
0
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:self.allPins.count];
[self.mapView addOverlay:polyline];

Check this http://pinkstone.co.uk/how-to-draw-an-mkpolyline-on-a-map-view/ and for swift http://rshankar.com/how-to-add-mapview-annotation-and-draw-polyline-in-swift/

Arvind
  • 493
  • 1
  • 8
  • 23