问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/19821940/put-mkpointannotation-on-mkdirections-at-a-specific-distance