【问题标题】:Put MKPointAnnotation on MKDirections at a specific distance将 MKPointAnnotation 放在特定距离的 MKDirections 上
【发布时间】:2013-11-06 20:17:15
【问题描述】:

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

例如,我有一个 MKMap,其中包含从 LA 到 NY 的 MKDirections。然后,我想在用户决定采取的路线的 10 英里标记处放置一个大头针。

对于如何在特定距离使用所选路线来找到纬度/经度有什么想法吗?

谢谢。

- (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;
            }
         }
    }
}

这很有效,因为我能够确定应该放置哪个步骤,但我的问题是如何确定步骤中的位置。

【问题讨论】:

  • 您尝试过什么吗> 如果有,请发布您的代码。
  • 我没有尝试任何关于获取纬度/经度的方法,但是我能够获得距离发生在哪个 MKRouteStep 中。有没有办法确定 step.polyline 中的特定点?
  • 看起来这需要一些工作:找出步骤中的哪两个坐标 (A,B) 包含 10 英里位置 (P)。 getCoordinates:range: 方法在这里可能会有所帮助。找出从 A 到 10 英里标记还需要多少英里。找到从 A 到 B 的方位(SDK 中没有方法)。最后,使用算法like this找到P的坐标。
  • 我担心这会是答案。我暗暗希望SDK中有一些超级秘密的方法。感谢你的回答。如果您想要信用,请您发布作为答案。

标签: iphone objective-c ios7 mapkit


【解决方案1】:

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

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

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    相关资源
    最近更新 更多