Example One
function distance ($lat 1, $lng 1, $lat 2, $lng 2, $miles = True)
{
$pi = m_pi/180;
$lat 1 *= $PI 80;
$LNG 1 *= $PI 80;
$lat 2 *= $PI 80;
$LNG 2 *= $PI 80;
$r = 6372.797;
$dlat = $lat 2-$lat 1;
$DLNG = $lng 2-$LNG 1;
$a = sin ($dlat/2) *sin ($dlat/2) +cos ($lat 1) *cos ($lat 2) *sin ($DLNG/2) *sin ($DLNG/2);
$c = 2 * atan2 (sqrt ($a), sqrt (1-$a));
$km = $r * $c;
Return ($miles? ($KM * 0.621371192): $km);
}
Example Two
/**
* Calculate the distance between two coordinates (m)
* @param float $fP 1Lat starting point (latitude)
* @param float $fP 1Lon starting point (longitude)
* @param float $fP 2Lat end point (latitude)
* @param float $fP 2Lon end point (longitude)
* @return int
*/
function Distancebetween ($fP 1Lat, $fP 1Lon, $fP 2Lat, $fP 2Lon) {
$fEARTH _radius = 6378137;
Angle converted into radians
$fRadLon 1 = Deg2rad ($fP 1Lon);
$fRadLon 2 = Deg2rad ($fP 2Lon);
$fRadLat 1 = Deg2rad ($fP 1Lat);
$fRadLat 2 = Deg2rad ($fP 2Lat);
Calculate the difference of longitude and latitude
$fD 1 = ABS ($fRadLat 1-$fRadLat 2);
$fD 2 = ABS ($fRadLon 1-$fRadLon 2);
Distance calculation
$fP = Pow (sin ($fD 1/2), 2) +
cos ($fRadLat 1) * cos ($fRadLat 2) * POW (sin ($fD 2/2), 2);
Return Intval ($fEARTH _radius * 2 * ASIN (SQRT ($fP)) + 0.5);
}
/**
* Baidu coordinate system converted into standard GPS seating system
* @param float $lnglat coordinates (for example: 106.426, 29.553404)
* @return The standard GPS value after string conversion:
*/
function bd09lltowgs84 ($fLng, $fLat) {//longitude, Latitude
$lnglat = Explode (', ', $lnglat);
List ($x, $y) = $lnglat;
$Baidu _server = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x={$x}&y={$y}";
$result = @file_get_contents ($Baidu _server);
$json = Json_decode ($result);
if ($json->error = = 0) {
$BX = Base64_decode ($json->x);
$by = Base64_decode ($json->y);
$GPS _x = 2 * $x-$BX;
$GPS _y = 2 * $y-$by;
return $GPS _x ', '. $GPS _y;//Longitude, latitude
}else
return $lnglat;
}