- SPEEDTRAP ALERT for Windows Phone 8
- Bing Maps
- Pushpins
- WP8 emulator tool
- Expression parser and evaluator (.NET, C#)
- Start - simple example
- Variables, properties, arrays and collections
- Functions and methods
- Operators
- Reference
- Something about iterating through UTF-16 Unicode strings (.NET, C#)
- Few words about toolkit:ExpanderView (XAML, .NET, C#, Windows Phone)
- Simple geo calculations
- Delphi (old works)
- Red-Black Tree. TAleRBTreeNode Delphi Class.
- Associative array. TAleAssociativeArray and TAleValue Delphi classes.

# Simple geo calculations

All calculations below are made in assumption of spherical shape of the Earth. This allows to simplify formulas and accelerate calculations, what is especially important for mobile devices because of low performance CPU’s and power consumption. The precision is sufficient for most applications.

## Distance between two locations with given longitude and latitude

The idea behind the calculation is to define the cosine of the angle between two vectors, starting at the center of the Earth and ending on the Earth’s surface in given locations. Vectors are of the same length that is equal to the Earth’s mean radius. Knowing the cosine of the angle and, therefore, the angle itself (in radians) and also the Earth’s radius the distance is calculated as the arc length of the sector formed by these vectors: L = R∙γ, where R - Earth’s radius, γ – angle in radians.

cos γ = cos α_{1} ∙ cos α_{2} ∙ (cos(β_{1} - β_{2}) - 1) + cos(α_{1} - α_{2})

where α_{1}, α_{2} – latitudes, β_{1}, β_{2} – longitudes.

L = R ∙ acos cos γ

Explanation of this formula:

## Initial heading from one location with given longitude and latitude to another

Heading is set in degrees in range 0-360° (0° – heading to the North). Its calculation is based on spherical law of cosines. A calculation of angle between vectors A and B is also required. Joining these calculations in one function gives benefit in performance when both distance and heading are needed.

Angle φ is a heading from location A to location B. α_{1}, α_{2} – angles between vectors formed by A and B and Earth’s axis (they are equal to π/2 minus latitude of locations), γ – angle between vectors A and B, N – Earth’s pole.

In these notations, spherical law of cosines looks like this:

cos α_{2} = cos α_{1} ∙ cos γ + sin α_{1} ∙ sin γ ∙ cos φ

cos φ = (cos α_{2} - cos α_{1} ∙ cos γ) / (sin α_{1} ∙ sin γ)

For calculation of cos γ see above.

### Special cases

sin α_{1} = 0 – this means that location A is at the Earth’s pole. If latitude of A is 90° (The North Pole), every direction from this location is to the South. Therefore φ = 180°. If latitude of A is -90° (The South Pole), every direction from this location is to the North. Therefore φ = 0°.

sin γ = 0 – this means that locations A and B coincide or that location B is exactly on the opposite side of the Earth. All directions are equal and angle φ can be arbitrary. Let it be a 0°.

In some cases, a calculated cos φ may be greater in absolute value than 1 for a very small value. So, Math.Acos(cos_phi) will return Double.NaN. Do the following check to avoid this:

if (Math.Abs(cos_phi) > 1) cos_phi = Math.Truncate(cos_phi);

Math.Acos(double) returns value in [0, π] interval. To get heading in [0, 2π] (or in 0-360°) it is necessary to do:

// lonA, lonB - longitudes of A and B in radians ... double heading = Math.Acos(cos_phi); if (Math.Abs(lonB - lonA) > Math.PI) { if (lonB > 0) heading = 2 * Math.PI - heading; } else if (lonB < lonA) heading = 2 * Math.PI - heading; ...