Ios 计算从横向/纵向坐标到街道的距离

Ios 计算从横向/纵向坐标到街道的距离,ios,math,geolocation,Ios,Math,Geolocation,我有一条街道的坐标,例如: 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 street[3];
street[0] = CLLocationCoordinate2DMake(-17.3521, 145.5898);
street[1] = CLLocationCoordinate2DMake(-17.3518, 145.5910);
street[2] = CLLocationCoordinate2DMake(-17.3515, 145.5917);
以及离街道相当近的位置(约60米):

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

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

编辑

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

  • 街道
    是三个红点
  • 位置
    是蓝点
我想以米为单位计算黄线的长度。

看看这个网站:


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

如果您有权查找两个位置之间的乌鸦距离,请将
CLLocation
对象设置为两个坐标,然后

CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];

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

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

// 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);
}
//http://www.cprogramto.com/c-program-to-find-shortest-distance-between-point-and-line-segment/
双FindInstanceToSegment(双x1、双y1、双x2、双y2、双点X、双点Y)
{
双扩散系数=x2-x1;
浮动差=y2-y1;
如果((diffX==0)和&(diffY==0))
{
diffX=点x-x1;
diffY=pointY-y1;
返回sqrt(diffX*diffX+diffY*diffY);
}
浮点t=((点x-x1)*diffX+(点y-y1)*diffY)/(diffX*diffX+diffY*diffY);
if(t<0)
{
//点距离第一个点最近,即x1和y1
diffX=点x-x1;
diffY=pointY-y1;
}
否则如果(t>1)
{
//点距离终点最近,即x2和y2
diffX=点x-x2;
diffY=pointY-y2;
}
其他的
{
//如果垂直线与线段相交。
diffX=点x-(x1+t*diffX);
diffY=pointY-(y1+t*diffY);
}
//返回最短距离
返回sqrt(diffX*diffX+diffY*diffY);
}
-

cllocationcoordinated2d街[3];
街道[0]=CLLocationCoordinate2DMake(-17.3521145.5898);
街道[1]=CLLocationCoordination2Dmake(-17.3518145.5910);
街道[2]=CLLocationCoordination2Dmake(-17.3515145.5917);
CLLocationCoordinate2D位置=CLLocationCoordinate2DMake(-17.3525145.5911);
CLLocationDegrees距离Degrees=CGFLOAT_最大值;
对于(整数nodeIndex=1;nodeIndex<3;nodeIndex++){
CLLOCATIONCoordinated2D NODECORD=街道[nodeIndex];
CLLocationCoordinate2D PrevNodeCord=街道[nodeIndex-1];
CLLocationDegrees distanceToCurrent=FindDistanceToSegment(PrevNodeCord.longitude,PrevNodeCord.latitude,NodeCord.longitude,NodeCord.latitude,NodeCord.latitude,location.longitude,location.latitude);
if(距离到电流<距离度)
距离度=距离到电流;
}
CLLocationDistance距离=距离度*111111;//1.0度约为111111米
NSLog(@“%f”,距离);//78.15米

你只能找到乌鸦距离,即位移要找到实际的道路距离,你必须找到直线上的坐标。我不清楚你到底想计算什么-你想找到你的街道相邻点和落在道路上的点之间的距离吗(由三个街道点创建的线)距离相邻点最近?@blabus是的,我想要从
位置
街道
中三个点所创建的线的距离。看看这个:我没有两个位置,我有四个位置。我想要计算从
位置
街道之间最近位置的距离[0]
街道[1]
。因此,您必须计算从
位置
到每个
街道
位置的距离,并计算所有距离,以找到最近的位置。如果不知道它们之间的距离,您无法找到。除了我需要沿路径的最近点,它可能不是数组中的一个点。它将是数组中的某个点中间。请看我编辑的问题和一张精美的图片。
// 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