将MKPointAnnotation放在特定距离的MKDirections上 [英] Put MKPointAnnotation on MKDirections at a specific distance

查看:101
本文介绍了将MKPointAnnotation放在特定距离的MKDirections上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MKDirections绘制从点A到点B的路线,并且我希望将MKPointAnnotation放置在距起点特定距离处.

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.

例如,我有一个带有从洛杉矶到纽约绘制的MKDirections的MKMap.然后,我想在用户决定采用的路线的10英里标记处放置一个图钉.

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?

谢谢.

- (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.

推荐答案

我将解释解决类似问题的方法.

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

您可以使用属性step.polyline.coordinate.它为您提供路线每一步终点的纬度和经度.当变量iDistance超过10英里时,此步骤的终点(或上一步的终点)将提供您要查找的近似坐标.无论如何,您的路段都将包含"10英里"距离.

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.

要获得更准确的坐标,您可以在此线段的端点和上一个线段的端点之间执行一些简单的三角运算,以计算距离并放置MKPointAnnotation.

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 .

希望对您有帮助.

这篇关于将MKPointAnnotation放在特定距离的MKDirections上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆