1

How to calculate location of the point (x,y) , that is far from point 1 (which has lat1 and lon1 coords) for distance d as shown in picture

enter image description here

Known parameters:
Point1 = [lat1,lon1],
Point2 = [lat2,lon2],
Distance = d, ( in meters )
Angle = α = atan( (lat2 - lat1) / (lon2 - lon1) )

Need to find: Destination Point: x and y value.

In one word, I need something like vice versa of this

CLLocation *location1;
CLLocation *location2; 
double distance =  [location1 distanceFromLocation:location2];

e.g. calculate location2 by given distance and angle.

What I've tried

double lat = location1.coordinate.latitude + distance * sin(alpha);
double lon = location1.coordinate.longitude + distance * cos(alpha);

But those values are wrong, because 1 latitude and 1 longitude not equal to 1 meters.

Larme
  • 24,190
  • 6
  • 51
  • 81
arturdev
  • 10,884
  • 2
  • 39
  • 67
  • I think the answer is using the Pythagorean theorem which might be ok flat surfaces and very short distances. For a curved surface and a calculation that will work better for larger distances, try this answer: http://stackoverflow.com/a/6634982/467105 –  May 23 '14 at 11:00

2 Answers2

3
    CLLocation Point1;
    CLLocation Point2;
    float targetDistance;

    float length = sqrt((Point1.x-Point2.x)*(Point1.x-Point2.x) + (Point1.y-Point2.y)*(Point1.y-Point2.y));

    CLLocation result;
    result.x = Point1.x + (Point2.x-Point1.x)*(targetDistance/length);
    result.y = Point1.y + (Point2.y-Point1.y)*(targetDistance/length);

Or in other words Point1 + normalized(Point2-Point1)*targetDistance

Since you have an angle you could also do:

Point1 + (cos(angle)*targetDistance, sin(angle)*targetDistance)
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • Thank you very much. I think I understand logic. But can you explain, why result.y = Point2.x +... and not Point1.x +... ? – arturdev May 23 '14 at 07:37
0

I wrote a detailed blog a while back that explains two different methods for doing this calculation and the mathematical proofs behind them. One method is to calculate the arcLength using cosine law, the other method is to use the Haversine formula. You can find the blog here: http://rbrundritt.wordpress.com/2008/10/14/calculate-distance-between-two-coordinates/

rbrundritt
  • 16,570
  • 2
  • 21
  • 46