9

I was just curious what the correct way to draw a simple route line between a set of points was? I currently have an array of coordinates and when I pass it to polylineWithCoordinates and do all the other necessary things, it draws a big web of lines that interconnect all of the points to one another. I've looked at a few samples but none of them seem to do anything special to account for this, even when they use more than two points.

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

    //Add drawing of route line
    CLLocationCoordinate2D coordinates[[myCheckpoints count]];

    int i = 0;
    for (Checkpoint *ckpt in myCheckpoints)
    {
        coordinates[i] = CLLocationCoordinate2DMake([ckpt.lat floatValue] , [ckpt.lon floatValue]);
        i++;
    }

    MKPolyline *route = [MKPolyline polylineWithCoordinates: coordinates count: [myCheckpoints count]];
    [mapView addOverlay:route];

}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    MKPolylineView *polylineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
    polylineView.strokeColor = [UIColor greenColor];
    polylineView.lineWidth = 5.0;

    return polylineView;

}

This is the code in my mapViewController that is responsible for the drawing, just in case somebody sees what I'm doing, or not doing. Here is what the result looks like

Now that I look at everything much closer, is actually not connecting adjacent coordinates to each other. Each point only has 2 lines stemming from it connecting that point to 2 more points but I cant figure out the pattern its connecting them in.

Karoly S
  • 3,180
  • 4
  • 35
  • 55
  • Do any of the coordinates repeat? Make sure the coordinates in myCheckpoints are in the order that you want the line drawn in. –  Aug 22 '11 at 16:02
  • Yea, all of the coordinates are unique and in the correct order. So I am right in assuming this is not supposed to be happening? – Karoly S Aug 22 '11 at 16:08
  • It will draw the lines in the order the coordinates are provided. How many points are in the line? Log the coordinates as they are added in the for-loop. –  Aug 22 '11 at 16:11
  • There are 20. I went ahead and logged them, none dublicated, etc. I'm not sure what it could be. – Karoly S Aug 22 '11 at 16:31
  • Can you please post the log? A small screenshot of the result may help also. –  Aug 22 '11 at 16:35
  • Added a picture as that is more helpful than the log. The log is literally just 20 pairs of floats representing lat/lon. Lon is all negative due to the points they actually represent, though that shouldn't matter. – Karoly S Aug 22 '11 at 16:48
  • Do any of the segments cross the Pacific Ocean? Can you reproduce the behavior with just say 4 points? –  Aug 22 '11 at 17:15
  • It may turn out to be that my points are actually not in order. When taking the first 4 coordinates, its not actually the first 4 in my route. I'm going to try sorting them and seeing if that fixes the problem. No crossing of the Pacific Ocean though, but I know that breaks it as well and have found an answer here on SO that contains a fix. Will post back after sorting with results. – Karoly S Aug 22 '11 at 17:23
  • 1
    @Anna Karenina: You were right, I thought that I was returned my points in order, but I was wrong. I have sorted them and now my line draws correctly. If you add your suggestions from the comments into the answers section I'll go ahead and mark it right :) – Karoly S Aug 24 '11 at 16:24
  • how u sort coordinates(lati,long)... – amit gupta Mar 16 '15 at 09:59
  • @Anna same issue I have to write in Swift, is there any example for that? – Anilkumar iOS - ReactNative Mar 14 '18 at 08:58
  • @KarolyS same issue I have to write in Swift, is there any example for that? – Anilkumar iOS - ReactNative Mar 14 '18 at 08:58

1 Answers1

3

Make sure the coordinates in myCheckpoints are in the order that you want the lines drawn in.

It will draw the lines in the order the coordinates are provided.

  • I used the block sorting method in the answer here: http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – rmooney May 07 '14 at 13:51
  • i get response from web service i didnt get any instruction how to draw a route . i am just pass the araay of cordinates and draw a route but problem is that it draw two line one from starting to end and other end to start point – amit gupta Mar 16 '15 at 11:04