You are here

function _location_nearby_postalcodes in Location 5

This is the main logic-level function for performing proximity postal-code searches. It calls a number of helper functions for finding postal_code results in each country, narrowing down results, formatting the returned array, and sorting the returned array. Finally, it implements a caching mechanism that will return a subset (proper or not) of a previous search's results on the same search-point.

Parameters

$lon: A floating point of the longitude coordinate about which the search is being executed

$lat: A floating point of the latitude coordinate about which the search is being executed

$distance : A floating point of the distance in meters; it is forced to an integer ceiling, but kept as a float

Return value

An array where -> the keys are a concatenation of the lower-case two-letter ISO country code and the postal code For example, 94063 in the United States would be 'us94063' -> the values are an associative array where 'postal_code' => A postal code that fell within the search-radius given by $distance and $distance_unit. 'country' => The two-letter ISO code for the home-country of this particular postal_code search result. 'lon' => The longitude coordinate of the approximate center of the area covered by 'postal_code' 'lat' => The latitude coordinate of the approximate center of the area covered by 'postal_code' 'distance' => The floating point distance of the approximate center of this 'postal_code' from the parameter lat/lon point in meters

2 calls to _location_nearby_postalcodes()
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 817

Code

function _location_nearby_postalcodes($lon, $lat, $distance) {
  $search_results = _location_search_results_from_cache($lon, $lat, $distance);

  // If there were usable cached search_results then return those and go no further... awwwww yeah.
  if (count($search_results)) {
    return $search_results;
  }

  //------------------------------------------------------------------------------------------

  // Pulling from the cache takes place right here.
  // The cache id ("cid", to be inserted into the cache table) will be a concatenation of
  //   -> "location_prox_search:".
  //   -> round($lon, 3) . ':'.
  //   -> round($lat, 3) . ":".
  //   -> $distance
  // The value of the cached item will be a serialize($result_array)
  //
  // The cache will be cleared of proximity searches when there is a change in countries that have
  // been configured into the system.

  //------------------------------------------------------------------------------------------
  $search_results = array();
  $latrange = earth_latitude_range($lon, $lat, $distance);
  $lonrange = earth_longitude_range($lon, $lat, $distance);

  //$query_start_time = microtime();
  $result = db_query('SELECT zip, city, state, country, ' . earth_distance_sql($lon, $lat) . ' as distance  FROM {zipcodes} WHERE latitude > %f AND latitude < %f AND longitude > %f AND longitude < %f AND ' . earth_distance_sql($lon, $lat) . ' < %f ORDER by distance', $latrange[0], $latrange[1], $lonrange[0], $lonrange[1], $distance);
  while ($result_row = db_fetch_object($result)) {
    $search_results[$result_row->country . $result_row->zip] = array(
      'city' => $result_row->city,
      'province' => $result_row->state,
      'distance' => $result_row->distance,
    );
  }

  //DEBUG: commented code for testing/debugging purposes

  //$query_end_time = microtime();

  //print 'TOTAL TIME FOR _location_nearby_postalcodes() '. _location_time_difference($query_end_time, $query_start_time) ."<br/>\n";

  //--------------------------------------------------------------------------------------------

  // This is the spot where search results are cached
  cache_set('location_prox_search:' . round($lon, 3) . ':' . round($lat, 3) . ':' . $distance, 'cache', serialize($search_results));

  // DEBUG: commented code is for testing/debugging purposes

  //print 'POSTAL CODE SEARCH CACHING: Wrote new search results to cache'."<br/>\n";

  //--------------------------------------------------------------------------------------------
  return $search_results;
}