1

I'm using MKDirections to draw a route from point A to point B, and I'm looking to put a MKPointAnnotation at specific distance from the starting point.

For example, I have a MKMap with MKDirections drawn from LA to NY. I then would like to place a pin at the 10 mile marker for the route the user has decided to take.

Any thoughts on how to find the lat/long, using the chosen route at a specific distance?

Thanks.

- (void)showDirections:(MKDirectionsResponse *)response
{
    NSInteger iDistance = 0;
    BOOL bPointinStep = NO;
    self.response = response;
    for (MKRoute *route in _response.routes)
    {
        NSLog(@"route.distance: %.0f", route.distance);
        responseNumber = responseNumber + 1;
        [_mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
        for (MKRouteStep *step in route.steps)
        {

            iDistance = iDistance + step.distance;

            NSLog(@"iDistance: %.0ld", (long)iDistance);
            NSLog(@"step.distance: %.0f", step.distance);
            NSLog(@"%@", step.instructions);
            if (iProgress < iDistance && !bPointinStep) {
                NSLog(@"pin point is on this step");
                bPointinStep = YES;
            }
         }
    }
}

This works, as I'm able to determine which step the pin point should be placed, but my question is on how to determine where within the step.

cdavis
  • 11
  • 2
  • Have you tried anything> Post your code if so. – Aleksander Lidtke Nov 06 '13 at 20:37
  • I haven't tried anything in regards to getting the lat/long, however I am able to get which MKRouteStep the distance occurs in. Is there a way to determine a specific point within a step.polyline? – cdavis Nov 06 '13 at 20:56
  • It looks like this will require some work: Find which two coordinates (A,B) within the step contain the 10-mile position (P). The getCoordinates:range: method may help here. Find out how many more miles you need to go from A to get to the 10-mile mark. Find the bearing from A to B (no method in SDK). Finally, find the coordinate of P using an algorithm [like this](http://stackoverflow.com/questions/6633850/calculate-new-coordinate-x-meters-and-y-degree-away-from-one-coordinate). –  Nov 06 '13 at 23:13
  • I was afraid that was going to be the answer. I was secretly hoping there was some super secret method in the SDK for this. Thanks for the answer. If you want credit, can you please post as an answer. – cdavis Nov 11 '13 at 19:15

1 Answers1

0

I'll explain my approach to solve a similar problem.

You can use the property step.polyline.coordinate. It gives you the latitude and longitude of the end point of each step of your route. When your variable iDistance exceeds the 10 miles, the end point of this step (or end point of the previous step) will give an approximate coordinate that you are looking for. In any case you will have the segment containing "10 miles" distance.

To obtain a more accurate coordinate you can perform some simple trigonometric operations between the end point of this segment and the endpoint of the previous segment, to calculate the distance and put the MKPointAnnotation .

I hope it helps you.

Ferran T.
  • 242
  • 3
  • 12