Using the Cosine Law to calculate the distance between two points in Objective-C?

I get some GPS coordinates from Google Maps and I need to find the distance between them using Objective C. I have implemented the formula, but I get results that are great ways.

I tested the values ​​from Google Maps, transferring them back to Google Earth and the geocoding service on the Internet, and everything is checked. Im now starting to suspect that the cosine law requires me to do some kind of transformation with coordinates before passing them.

I did a similar implementation of the Haversin formula, but it also gave me great results. Then I switched to cosine, since it was easier to debug, and I did not need very high accuracy.

Hope someone can shed some light on this or use the code :)

- (CGFloat) calculateDistanceBetweenPoints:(CGPoint) origin andDestination:(CGPoint) destination {

//To convert kilometers to miles, divide by 1.609
// x = latitude 
// y = longitude

/* example:
 Dubai      : 25.248665, 55.352917 
 Amsterdam  : 52.309071, 4.763385
 Approx dist: 5,182.62 KM
 Calc. dist : 8,253.33
 */

CGFloat toRad           =   (M_PI / 180);
CGFloat R               =   6371.0f; //earth mean radius in Km

CGFloat sinePart        =   sinf( origin.x * toRad ) * sinf( destination.x * toRad );
CGFloat cosinePart      =   cosf( origin.x * toRad ) * cosf( destination.x * toRad );
CGFloat deltaCosinePart =   cosf( ( destination.y - origin.y ) * toRad );

CGFloat delta           =   acosf( sinePart + cosinePart * deltaCosinePart) * R;

return delta;
}

Above calculated from the links given here: the question about the stack surface

+3
source share
5 answers

The code may be ok when I run it in your example data (minus a few decimal places), it returns 5168.3584

+2
source

Do you consider using the method presented in CLLocation:

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location?

+3
source

, , :

, A , B C a = 90 ° - φ Ams, b = 90 ° - φ Dub, C = Δλ = λ Dub - λ Ams. - c.

SO 389211.

( - , , .)


ASCII :

                   + C (North Pole)
                  /|
                b/ |
                /  |
(Amsterdam) A  +   | a
                \  |
                c\ |
                  \|
                   + B (Dubai)

:

cos c = cos a . cos b + sin a . sin b . cos C

, cos (90º - x) = sin x sin (90º - x) = cos x, :

cos c = sin φ Ams. sin φ Dub + cos φ Ams. cos φ Dub. cos Δλ

c .


:

Dubai: φ Dub= 25.248665°N, λ Dub= 55.352917°E
Amsterdam: φ Ams= 52.309071°N, λ Ams= 4.763385°E

Δλ = 50.589532°

6 :

cos c = 0.426548 × 0.791320 + 0.904465 × 0.611402 × 0.634872
      = 0.337536            + 0.351079
      = 0.688615

:

c = 46.479426°
  =  0.811219 radians

6371 ,

c = 5168 km

, R = 6371 0,811219 × 6371 = 5168 ( 4 ..).

TrueKnowledge , 5155 . , , , , , . - , , .


+3

:( . . (x, y) , . :

- (CGPoint) translateToPixelsFromLatitude:(CGFloat) latitude andLongitude:(CGFloat) longitude {

CGPoint position    = CGPointMake(0, 0);
CGFloat mapWidth    = 300.0f;
CGFloat mapHeight   = 200.0f;

CGFloat offsetX     = 5.0f;
CGFloat offsetY     = 35.0f;

position.x = (((180 + longitude) / 360) * mapWidth) + offsetX;
position.y = (mapHeight - (((90 + latitude) / 180) * mapHeight)) + offsetY;

return position;

}

, , SO, , .

, , , hic3456 .

, , .

, -, , .

.

+1

Well, if I understood the question correctly, you do not take into account the curvature of the earth. The law of cosine works on a plane, not on a sphere. For example: the North Pole and the South Pole are located at a distance of ~ 20,000 km from each other if you are flying, but only ~ 6,000 km if you are digging a tunnel;)

Regards, Ari

0
source

Source: https://habr.com/ru/post/1728242/


All Articles