This topic is locked
[SOLVED]

 Calculate Distance Between Two Points

6/1/2013 10:26:19 AM
PHPRunner General questions
G
g_parry author

I have a field called distance that I need to populate with the distance between two postcodes. I have created 4 fields that hold the latitude and longitude co-ordinates of the postcode. These are hlat hlon wlat and wlon - I have placed the following code in the custom section of the field properties box

{

$pi80 = M_PI / 180;

$data["hlat"] *= $pi80;

$data["hlon"] *= $pi80;

$data["wlat"] *= $pi80;

$data["wlon"] *= $pi80;

$value =



$r = 6372.797; // mean radius of Earth in km

$dlat = $data["wlat"] - $data["hlat"];

$dlng = $data["wlon"] - $data["hlon"];

$a = SIN($dlat / 2) * SIN($dlat / 2) + COS($data["hlat"]) * COS($data["wlat"]) * SIN($dlng / 2) * SIN($dlng / 2);

$c = 2 * ATAN2(SQRT($a), SQRT(1 - $a));

$km = $r * $c;

RETURN ($miles ? ($km * 0.621371192) : $km);


I'm getting nothing - any help would be appreciated.
Cheers

Admin 6/1/2013

It looks like some syntax errors are in your code. Also you need to assign result to $value variable instead of returning it. Try something like this:

$pi80 = M_PI / 180;

$data["hlat"] *= $pi80;

$data["hlon"] *= $pi80;

$data["wlat"] *= $pi80;

$data["wlon"] *= $pi80;


$r = 6372.797; // mean radius of Earth in km

$dlat = $data["wlat"] - $data["hlat"];

$dlng = $data["wlon"] - $data["hlon"];

$a = SIN($dlat / 2) * SIN($dlat / 2) + COS($data["hlat"]) * COS($data["wlat"]) * SIN($dlng / 2) * SIN($dlng / 2);

$c = 2 * ATAN2(SQRT($a), SQRT(1 - $a));

$km = $r * $c;

$value = ($miles ? ($km * 0.621371192) : $km);