DISTANCE CALCULATE BETWEEN TWO POINT
Want to able to calculate distance between two latitude and
longitude ?
Function below to use two latitude and longitude point and
calculate distance in
Kilometer , Meter , Yard ,
Miles and Feet ...
function getDistanceBetweenPoints($latitude1, $longitude1, $latitude2, $longitude2) {
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return array('miles' => $miles, 'feet' => $feet, 'yards' => $yards, 'kilometers' => $kilometers, 'meters' => $meters);
}
CALCULATE DISTANCE FROM QUERY BETWEEN LAT/LONG
If we have already Lat/Long in our Table and want to find out disance from other Lat/Long
Simply we can find Distance From Select Query also its Good Practise for avoid lazy timing.
Simply we can find Distance From Select Query also its Good Practise for avoid lazy timing.
$user_lattitude = 'xxxx.xxxx';
$user_longitude = 'xxxx.xxxx';
$search_data = array(
latitude => $user_lattitude,
longitude => $user_longitude
);
QUERY :
SELECT ((ACOS(SIN(' . $this->db->escape($search_data['latitude']) . ' * PI() /180) * SIN(max(latitude) * PI() /180) + COS(' . $this->db->escape($search_data['latitude']) . ' * PI() /180) * COS(max(latitude) * PI() /180) * COS(( ' . $this->db->escape($search_data['longitude']) . ' - max(res.longitude) ) * PI() /180)) *180 / PI()) *60 * 1.1515 *1.609344 ) AS distance,
res.latitude,
res.longitude
FROM TABLENAME res