You are here

function location_distance_between in Location 7.5

Same name and namespace in other branches
  1. 5.3 location.inc \location_distance_between()
  2. 5 location.inc \location_distance_between()
  3. 6.3 location.inc \location_distance_between()
  4. 7.3 location.inc \location_distance_between()
  5. 7.4 location.inc \location_distance_between()

Given two points in lat/lon form, returns the distance between them.

Parameters

$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

$latlon_b: Another point formatted like $latlon_b

$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

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 130

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,
  );
}