2

I am trying to get two points, A and B, that are each in 5km distance horizontaly and verticaly around a center point C. Thus points A and B create a "box" with a side-length of 10km and C is in the exact center of this box. This is my code:

//Distance is 5 kilometers
float distance = 5;    

float latCenter = locationInfo.lastLat;
float lngCenter = locationInfo.lastLong;

// Calculate corner point distance in lat and long
float latDelta = distance/110.54f;
float longDelta = distance/(111.320f(float)Math.cos((double)latCenter));

// Calculate coordinates of the corner points
float latA = lat - latDelta;
float longA = lng - longDelta;
float latB = lat + latDelta;
float longB = lng + longDelta;

I know these are approximations, but approximating with a margin of error of ~1km is fine in my case. The vertical distance is fine (10.06km) but the horizontal distance is only about 6km instead of 10km.

What is wrong with my formula? I guess its some very simple math error, but I just can't find it. I needed a very simple and quick code, so I based my calculations on this StackOverflow answer: Simple calculations for working with lat/lon + km distance?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51

1 Answers1

3

I would bet on degres vs radians (from the javadoc of cos):

public static double cos(double a)

Returns the trigonometric cosine of an angle. Special cases: If the argument is NaN or an infinity, then the result is NaN. A result must be within 1 ulp of the correctly rounded result. Results must be semi-monotonic.

Parameters: a - an angle, in radians.

Returns: the cosine of the argument.