如何确定导航路线中的下一个POI? [英] How to determine the next POI in a navigation route?

查看:121
本文介绍了如何确定导航路线中的下一个POI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条路线(从Apple的MKDirections API中检索的MKRoute派生的MKPolyline)和靠近该路线的一堆兴趣点(MKAnnotation s数组).

I have a route (MKPolyline derived from an MKRoute retrieved from Apple's MKDirections API) and a bunch of points of interest (array of MKAnnotations) that are close to the route.

我想知道如何选择用户要遵循的路线下一个POI,以便通过UI进行展示.

I would like to know how to select the next POI the user is going to meet following the route, in order to present it through a UI.

我想到了两种不同的方法,但没有一种是真正足够的:

Two different approaches come to mind, but none of them is really adequate:

  • 第一个是在每次距离POI足够近时将其标记为已选中,并仅在数组中显示第一个未标记的POI(我们假设它们的顺序正确).问题是,如果由于某种原因或其中一个POI未选中,则该应用将永远显示该POI,而不是显示实际的下一个POI.例如,这种情况可能出现.如果用户遵循的路线与建议的路线略有不同,则该路线距离POI不够近,无法对其进行检查;或者用户在第一个POI之后开始导航;等等

  • The first one would be to mark the POIs as checked each time you get close enough to them, and simply display the first unmarked POI in the array (we'll assume that they are correctly ordered). The problem is that if for a reason or another one of the POIs is not checked, then the app will forever display it instead of displaying the actual next POI coming. This situation can arise e.g. if the user followed a slightly different route than the one suggested, that didn't come close enough to the POI to get it checked; or the user starts the navigation after the first POI; etc.

第二个选择是选择最接近用户的POI(可能还带有标记系统,以避免显示您刚刚检查过的POI).但这仅适用于足够笔直的路线:有时在山区或其他蜿蜒路线上,您可能会更接近一点,稍后您将实际越过该点.我希望这种情况实际上经常发生.

The second one one would be to select the POI closest to the user (probably also with a marking system to avoid presenting the POI you just checked). But this would only work for routes straight enough: sometimes in mountain zones or other sinuous routes you can get closer to a point that you will actually cross later. I expect this situation to happen actually quite often.

有什么主意吗?

推荐答案

当我不得不在我们的一个应用中实施转弯操作时,我使用了您所描述的第一个项目符号.为了弄清楚用户是否与原始折线不同,我每次读取新位置时都会计算当前位置和线段之间的距离.一旦检测到我没有遵循该路径,便会在向用户显示正在重新计算..."消息时重新计算该路径.

When I had to implement a turn-by-turn in one of our apps I used what you describe as first bullet. To figure out if user diverged from original polyline I calculated distance between current position and line segments each time I read a new position. Once I detected I was NOT following the path, I re-calculated the route while showing a "Recalculating..." message for user.

这是我的代码

- (BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate closeToPolyline:(MKPolyline *)polyline {
  CLLocationCoordinate2D polylineCoordinates[polyline.pointCount];
  [polyline getCoordinates:polylineCoordinates range:NSMakeRange(0, polyline.pointCount)];

  for (int i = 0; i < polyline.pointCount - 1; i++) {
    CLLocationCoordinate2D a = polylineCoordinates[i];
    CLLocationCoordinate2D b = polylineCoordinates[i + 1];

    double distance = [self distanceToPoint:MKMapPointForCoordinate(coordinate) fromLineSegmentBetween:MKMapPointForCoordinate(a) and:MKMapPointForCoordinate(b)];
    if (distance < 25) {
      return YES;
    }
  }

  return NO;
}

- (double)distanceToPoint:(MKMapPoint)p fromLineSegmentBetween:(MKMapPoint)l1 and:(MKMapPoint)l2 {
  double A = p.x - l1.x;
  double B = p.y - l1.y;
  double C = l2.x - l1.x;
  double D = l2.y - l1.y;

  double dot = A * C + B * D;
  double len_sq = C * C + D * D;
  double param = dot / len_sq;

  double xx, yy;

  if (param < 0 || (l1.x == l2.x && l1.y == l2.y)) {
    xx = l1.x;
    yy = l1.y;
  }
  else if (param > 1) {
    xx = l2.x;
    yy = l2.y;
  }
  else {
    xx = l1.x + param * C;
    yy = l1.y + param * D;
  }

  return MKMetersBetweenMapPoints(p, MKMapPointMake(xx, yy));
}

然后我呼叫- (BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate closeToPolyline:(MKPolyline *)polyline {,其中coordinate是用户的当前位置,而polyline是您MKDirections的路径.

Then I call - (BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate closeToPolyline:(MKPolyline *)polyline { with coordinate being users current location and polyline being path from your MKDirections.

就我而言,我不允许超过25米的距离,但这可能取决于您的经纬度精度.

In my case I wouldn't allow more than 25 meters off but it might depend on your lat/lng precision.

也许会帮助您或某人.

这篇关于如何确定导航路线中的下一个POI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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