0

I have the following code, with which i am trying to draw a polyline between a set of coordinates (which are correct as I also use them to add pins to the map, and those work fine).

I call a drawing method to initiate the drawing like so (the array in the method call contains the necessary coordinates):

 [self drawRoute:[[transportData objectForKey:@"19"] objectForKey:@"stops"]];

This is the actual method that is supposed to draw the line on the map (selectedRoute is an MKPolyline object):

- (void)drawRoute:(NSArray *)routePointsArray {
if (selectedRoute) {
    [mapView removeOverlay:selectedRoute];
    selectedRoute = nil;
}

CLLocationCoordinate2D routeCoordinates[routePointsArray.count];
for (int i = 0; i < routePointsArray.count; i++) {
    float latitude = [[[routePointsArray objectAtIndex:i] objectForKey:@"lat"] floatValue];
    float longitude = [[[routePointsArray objectAtIndex:i] objectForKey:@"lon"] floatValue];
    CLLocationCoordinate2D routePoint =  CLLocationCoordinate2DMake(latitude, longitude);
    routeCoordinates[i] = routePoint;
}

selectedRoute = [MKPolyline polylineWithCoordinates:routeCoordinates count:routePointsArray.count];
[mapView addOverlay:selectedRoute];
[mapView setVisibleMapRect:[selectedRoute boundingMapRect]];
}

And this is my delegate:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];

if(overlay == selectedRoute)
{
    if(nil == routeLineView)
    {
        routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];
        routeLineView.fillColor = [UIColor redColor];
        routeLineView.strokeColor = [UIColor redColor];
        routeLineView.lineWidth = 5;

    }

    return routeLineView;
}

return nil;
}

I kind of narrowed it down to the routeCoordinates array not getting filled up with coordinates, but I do not understand why.

Also, if you spot any mistakes in the code I would really appreciate if you could point those out to me (possibly with a solution) as I am just learning this part of iOS and can use any help I can get.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
Gergely Kovacs
  • 1,045
  • 2
  • 10
  • 28
  • You create an empty `routeLineView` but then compare it to `nil` (which it won't be) before adding the overlay line. – Paulw11 May 08 '18 at 21:27
  • YEah, I do not quite understand that part. But you are right, if I take out that condition, the line does get drawn. – Gergely Kovacs May 08 '18 at 21:51

1 Answers1

0

You have an error in your rendererForOverlay method.

The first thing it does is assign an instance of MKPolylineRenderer to routeLineView, but later you only actually add the overlay if routeLineView is nil, which it won't be.

Remove the line that assigns the initial value to routeLineView.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • If I remove the line, the compiler gives me a "use of undeclared identifier" error. – Gergely Kovacs May 09 '18 at 04:08
  • You need to keep the declaration, but remove the initialisation. – Paulw11 May 09 '18 at 04:09
  • Okay, it works this way. However, it brings up the question of why do I even need the nil condition since routeLineView will always be nil. I don't understand the logic behind this condition. If you could please provide me with a properly working rendererForOverlay method that is also in accordance with Apple guidelines, I will accept your answer. I based my code on this answer: https://stackoverflow.com/questions/10598322/iphone-how-to-draw-line-between-two-points-on-mapkit – Gergely Kovacs May 09 '18 at 04:30
  • Note that in that answer they are using a property, not a local variable, so they are checking that there isn't already an overlay shown – Paulw11 May 09 '18 at 04:35