Question

I have the coordinates for a street, for example:

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

And a location that is fairly close to the street (about 60 meters):

CLLocationCoordinate2D location = CLLocationCoordinate2DMake(-17.3525, 145.5911);

How can I calculate the distance between the location and position along the street's path?

I'm not looking for the distance to the closest point in the array, I want the distance to the closest position in between the points.

EDIT

It's easier to describe my question with a picture:

whats going on

  • street is the three red dots
  • location is the blue dot

I want to calculate the length of the yellow line in meters.

Was it helpful?

Solution 3

Here is my attempt to solve this, but I'm not sure if it's the best way?

// 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

OTHER TIPS

Take a look at this site: link.

It shows different types of distance measuring with latitude and longitude coordinates and even some code examples (in javascript).

If you have the find the crow distance between two locations, make CLLocation object of two coords, then

CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];

and if you have find the actual road distance divide the two coords to several coords in straight line and find the distance and add them up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top