<?php
/*
This Software calculates distance in km between 2 coordinates.
Copyright (C) 2020 Guido Richter / tintenkobold.de
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License [http://www.gnu.org/licenses/] for more details.
*/
/* calculate distance in km between 2 coordinates. when mode=SQL is set, a usable query string will be returned.
x2 and y2, x1 and y1 have to be column names of the sql table */
function distance_2coordinates($x1,$y1,$x2,$y2,$mode=0)
{
$f = 0.017453292519943;
$r = 6370;
if($mode=='SQL')
{
$ret = 'ACOS(SIN('.$x1.'*PI()/180)*SIN('.$y2.'*PI()/180)+COS('.$x1.'*PI()/180)*COS('.$y2.'*PI()/180)*COS(('.$x2.'*PI()/180)-'.$y1.'*PI()/180))*'.$r;
return $ret;
}
else
{
$a_lat = $y1 * $f;
$a_lon = $x1 * $f;
$b_lat = $y2 * $f;
$b_lon = $x2 * $f;
return acos(sin($b_lat)*sin($a_lat)+cos($b_lat)*cos($a_lat)*cos($b_lon-$a_lon))*$r;
}
}
?>