function location_distance_between in Location 7.3
Same name and namespace in other branches
- 5.3 location.inc \location_distance_between()
- 5 location.inc \location_distance_between()
- 6.3 location.inc \location_distance_between()
- 7.5 location.inc \location_distance_between()
- 7.4 location.inc \location_distance_between()
Given two points in lat/lon form, returns the distance between them.
Parameters
array $latlon_a: An associative array where 'lon' => is a floating point of the longitude coordinate for the point given by latlonA 'lat' => is a floating point of the latitude coordinate for the point given by latlonB
array $latlon_b: Another point formatted like $latlon_b
string $distance_unit: A string that is either 'km' or 'mile'. If neither 'km' or 'mile' is passed, the parameter is forced to 'km'
Return value
array|null NULL if sense can't be made of the parameters. An associative array where 'scalar' => Is the distance between the two lat/lon parameter points 'distance_unit' => Is the unit of distance being represented by 'scalar'. This will be 'km' unless 'mile' is passed for the $distance_unit param
Related topics
File
- ./
location.inc, line 229 - Public API for the Location module.
Code
function location_distance_between($latlon_a = array(), $latlon_b = array(), $distance_unit = 'km') {
if (!isset($latlon_a['lon']) || !isset($latlon_a['lat']) || !isset($latlon_b['lon']) || !isset($latlon_b['lat'])) {
return NULL;
}
if ($distance_unit != 'km' && $distance_unit != 'mile') {
return NULL;
}
// $conversion_factor = number to divide by to convert meters to $distance_unit
// At this point, $distance_unit == 'km' or 'mile' and nothing else
// $conversion_factor = ($distance_unit == 'km') ? 1000.0 : 1609.347;
$meters = earth_distance($latlon_a['lon'], $latlon_a['lat'], $latlon_b['lon'], $latlon_b['lat']);
return array(
'scalar' => round($meters / ($distance_unit == 'km' ? 1000.0 : 1609.347), 1),
'distance_unit' => $distance_unit,
);
}