【问题标题】:Calculate distance from a lat/lon coord to a street计算从纬度/经度坐标到街道的距离
【发布时间】:2014-05-01 07:05:19
【问题描述】:

我有一条街道的坐标,例如:

CLLocationCoordinate2D street[3];
street[0] = CLLocationCoordinate2DMake(-17.3521, 145.5898);
street[1] = CLLocationCoordinate2DMake(-17.3518, 145.5910);
street[2] = CLLocationCoordinate2DMake(-17.3515, 145.5917);

而且位置离街道相当近(约 60 米):

CLLocationCoordinate2D location = CLLocationCoordinate2DMake(-17.3525, 145.5911);

如何计算位置与街道路径上的位置之间的距离?

我不是在寻找到数组中最近点的距离,我想要的是到点之间最近位置的距离。

编辑

用图片来描述我的问题更容易:

  • street 是三个红点
  • location 是蓝点

我想以米为单位计算黄线的长度。

【问题讨论】:

  • 只能求乌鸦距离即位移
  • 要找到实际的道路距离,您必须找到直线上的坐标
  • 我不清楚您到底要计算什么 - 您是否想找到与街道相邻的点与落在道路上的点之间的距离(由三个街道点)最接近该相邻点?
  • @blabus 是的,我想要从location 到由street 中的三个点创建的线的距离。

标签: ios math geolocation


【解决方案1】:

看看这个网站:link

它使用经纬度坐标显示不同类型的距离测量,甚至还有一些代码示例(在 javascript 中)。

【讨论】:

    【解决方案2】:

    如果你有两个位置之间的距离,则将CLLocation对象设为两个坐标,然后

    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
    

    如果你找到了实际的道路距离,将两个坐标分成几条直线,然后找到距离并将它们相加。

    【讨论】:

    • 我没有两个位置,我有四个位置。我想计算从locationstreet[0]street[1] 之间最近位置的距离。
    • 因此,您必须计算从location 到每个street 位置的距离并全部计算,以找到最近的位置。不知道它们之间的距离是找不到的。
    • 除了我需要沿路径最近的点,这可能不是数组中的点之一。这将是介于两者之间的东西。用精美的图片查看我编辑的问题。
    【解决方案3】:

    这是我解决这个问题的尝试,但我不确定这是否是最好的方法?

    // http://www.cprogramto.com/c-program-to-find-shortest-distance-between-point-and-line-segment/
    double FindDistanceToSegment(double x1, double y1, double x2, double y2, double pointX, double pointY)
    {
      double diffX = x2 - x1;
      float diffY = y2 - y1;
      if ((diffX == 0) && (diffY == 0))
      {
        diffX = pointX - x1;
        diffY = pointY - y1;
        return sqrt(diffX * diffX + diffY * diffY);
      }
    
      float t = ((pointX - x1) * diffX + (pointY - y1) * diffY) / (diffX * diffX + diffY * diffY);
    
      if (t < 0)
      {
        //point is nearest to the first point i.e x1 and y1
        diffX = pointX - x1;
        diffY = pointY - y1;
      }
      else if (t > 1)
      {
        //point is nearest to the end point i.e x2 and y2
        diffX = pointX - x2;
        diffY = pointY - y2;
      }
      else
      {
        //if perpendicular line intersect the line segment.
        diffX = pointX - (x1 + t * diffX);
        diffY = pointY - (y1 + t * diffY);
      }
    
      //returning shortest distance
      return sqrt(diffX * diffX + diffY * diffY);
    }
    

    -

    CLLocationCoordinate2D street[3];
    street[0] = CLLocationCoordinate2DMake(-17.3521, 145.5898);
    street[1] = CLLocationCoordinate2DMake(-17.3518, 145.5910);
    street[2] = CLLocationCoordinate2DMake(-17.3515, 145.5917);
    
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake(-17.3525, 145.5911);
    
    
    CLLocationDegrees distanceDegrees = CGFLOAT_MAX;
    for (NSUInteger nodeIndex = 1; nodeIndex < 3; nodeIndex++) {
      CLLocationCoordinate2D nodeCoord = street[nodeIndex];
      CLLocationCoordinate2D prevNodeCoord = street[nodeIndex - 1];
    
      CLLocationDegrees distanceToCurrent = FindDistanceToSegment(prevNodeCoord.longitude, prevNodeCoord.latitude, nodeCoord.longitude, nodeCoord.latitude, location.longitude, location.latitude);
    
      if (distanceToCurrent < distanceDegrees)
        distanceDegrees = distanceToCurrent;
    }
    
    CLLocationDistance distance = distanceDegrees * 111111; // 1.0 degree is approximately 111,111 meters
    
    NSLog(@"%f", distance);  // 78.15 meters
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 2016-02-03
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2016-01-25
      • 2016-06-22
      相关资源
      最近更新 更多