You are here

function _location_format_search_result_distances in Location 5

Helper function: This function EXPECTS an array that has been returned by _location_sort_proximity_search_results()

Parameters

$results_array: An array of proximity search results where -> the keys are integer rankings starting from 1 for the closest postal_code search result -> the values are associative arrays where 'postal_code' => is the postal code of the search result 'country' => is the two-letter ISO of the country 'lon' => is the longitude coordinate of approximate center of the postal code 'lat' => is the latitude coordinate of the approximate center of the postal code 'distance' => is the distance of the approximate center of the result-postal_code from the approximate center of the postal_code about which the search was executed. This distance is in meters.

$distance_unit: A string abbreviation of a unit of distance. This must be either 'mile' or 'km'. If the argument is neither, it is forced to take the value of 'km'

Return value

Returns nothing. Modifies parameter $results_array so that 'distance' (mentioned above) now points to a string representation of distance that goes to 1 decimal place, AFTER it has been converted from the original meters to the distance unit specified by $distance_unit. Also adds a key 'distance_unit' => 'mile' or 'km' which reflects the $distance_unit parameter.

2 calls to _location_format_search_result_distances()
location_nearby_postalcodes_bylatlon in ./location.inc
Takes a latitude, longitude, and a distance, and returns all postal_codes within
location_nearby_postalcodes_bylocation in ./location.inc
Takes an location and a distance and returns an array of all postal-codes (from all countries that are supported) within the specified distance of the specified location.

File

./location.inc, line 980

Code

function _location_format_search_result_distances(&$results_array, $distance_unit = 'km') {
  if ($distance_unit != 'km' && $distance_unit != 'mile') {
    $distance_unit = 'km';
  }

  // $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;
  foreach ($results_array as $index => $single_result) {
    $results_array[$index]['distance'] = round($single_result['distance'] / $conversion_factor, 1);
  }
}