You are here

function location_latlon_rough_default in Location 7.5

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

Returns a lat/lon pair of the approximate center of the given postal code in the given country This function is a default implementation, in case the country support doesn't implement a proper function for this.

Parameters

$location: An associative array $location where 'street' => the street portion of the location 'supplemental' => additional street portion of the location 'province' => the province, state, or territory 'country' => lower-cased two-letter ISO code (REQUIRED) 'postal_code' => the international postal code for this location (REQUIRED)

Return value

An associative array where 'lat' => approximate latitude of the center of the postal code's area 'lon' => approximate longitude of the center of the postal code's area

1 call to location_latlon_rough_default()
location_latlon_rough in ./location.inc
Takes an location and returns a "rough" latitude/longitude pair based on the postal code data available for the given country.

File

./location.inc, line 294

Code

function location_latlon_rough_default($location = array()) {
  if (!isset($location['country']) || !isset($location['postal_code'])) {
    return NULL;
  }
  $query = db_select('zipcodes', 'z');
  $query
    ->addField('z', 'latitude', 'lat');
  $query
    ->addField('z', 'longitude', 'lon');
  $coords = $query
    ->condition('country', $location['country'])
    ->condition('zip', $location['postal_code'])
    ->execute()
    ->fetchAssoc();
  if ($coords) {
    return $coords;
  }
  elseif (($newlatlong = location_latlon_exact($location)) != NULL) {

    // try a one-time external geocoding
    if ($newlatlong['lat']) {

      // and store results in zipcodes table for next lookups being internally handled
      // (yeah this is missing city/state info a.t.m., but is way better than nothing!)
      db_insert('zipcodes')
        ->fields(array(
        'latitude' => $newlatlong['lat'],
        'longitude' => $newlatlong['lon'],
        'country' => $location['country'],
        'zip' => $location['postal_code'],
      ))
        ->execute();
    }
    return $newlatlong;
  }
  else {
    return NULL;
  }
}