Frage

Below is a formula (Great Circle algorithm) that I want to be translated into PHP. I would really appreciate if someone would help me with that?

The input is to different sets of coordinates, e.g:

Place 1 Lat: 59.389057
Place 1 Long: 17.937422

Place 2 Lat: 59.388914
Place 2 Long: 17.920441

The wanted output is the distance between place 1 and place 2.

distance = ((115.1666667 * (lat2-lat1)) ^ 2 + (115.1666667 * (lng2 - lng1) * cos(lat2 / 57.3)) ^ 2) ^ .5

My try:

$distance = pow(((115.1666667 * ($lat2 - $lat1)), 2) + (115.1666667 * pow(($lng2 - $lng1) * cos($lat2 / 57.3)), 2)), 0.5)
War es hilfreich?

Lösung

As DCoder writes in the comments, temporary variables make things easier to read:

$latD = (691 / 6) * ($lat2 - $lat1);
$lngD = (691 / 6) * ($lng2 - $lng1) * cos($lat2 / 57.3);

$distance = sqrt( $latD * $latD + $lngD * $lngD );

(demo on codepad.org)

Andere Tipps

Here's the corrected code:

$distance = pow((pow((115.1666667 * ($lat2 - $lat1)), 2) + pow((115.1666667 * ($lng2 - $lng1) * cos($lat2 / 57.3)), 2)), 0.5);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top