You are here

function _location_geo_logic in Location 6.3

Same name and namespace in other branches
  1. 5.3 location.module \_location_geo_logic()
  2. 7.5 location.module \_location_geo_logic()
  3. 7.3 location.module \_location_geo_logic()
  4. 7.4 location.module \_location_geo_logic()

Perform geocoding logic, etc., prior to storing in the database.

2 calls to _location_geo_logic()
location_load_location in ./location.module
Load a single location by lid.
location_save in ./location.module
Save a location.

File

./location.module, line 1413
Location module main routines. An implementation of a universal API for location manipulation. Provides functions for postal_code proximity searching, deep-linking into online mapping services. Currently, some options are configured through an…

Code

function _location_geo_logic(&$location, $changed, $filled, $inhibit_geocode = FALSE) {
  if (!$inhibit_geocode) {

    // Have any of the fields possibly affecting geocoding changed?
    // Or, was the location previously user submitted but is no longer?
    if ($changed['street'] || $changed['additional'] || $changed['city'] || $changed['province'] || $changed['country'] || $changed['postal_code'] || $location['source'] == LOCATION_LATLON_USER_SUBMITTED) {

      // Attempt exact geocoding.
      if ($data = location_latlon_exact($location)) {
        $location['source'] = LOCATION_LATLON_GEOCODED_EXACT;

        // @@@ How about an accuracy field here?
        $location['latitude'] = $data['lat'];
        $location['longitude'] = $data['lon'];

        // @@@ How about address normalization?
      }
      elseif ($data = location_get_postalcode_data($location)) {
        $location['source'] = LOCATION_LATLON_GEOCODED_APPROX;
        $location['latitude'] = $data['lat'];
        $location['longitude'] = $data['lon'];
      }
      else {
        $location['source'] = LOCATION_LATLON_UNDEFINED;
        $location['latitude'] = 0;
        $location['longitude'] = 0;
      }
    }
  }

  // Normalize coordinates.
  while ($location['latitude'] > 90) {
    $location['latitude'] -= 180;
  }
  while ($location['latitude'] < -90) {
    $location['latitude'] += 180;
  }
  while ($location['longitude'] > 180) {
    $location['longitude'] -= 360;
  }
  while ($location['longitude'] < -180) {
    $location['longitude'] += 360;
  }

  // If city and/or province weren't set, see if we can fill them in with
  // postal data OR if the city and/or province aren't configured to be
  // collected through the form, set them to whatever the postal code data
  // says they should be.
  if (!empty($location['postal_code'])) {
    if (empty($location['city']) || empty($location['province']) || empty($location['location_settings']['form']['fields']['city']['collect']) || empty($location['location_settings']['form']['fields']['province']['collect'])) {
      if ($data = location_get_postalcode_data($location)) {
        $location['city'] = empty($location['city']) || empty($location['location_settings']['form']['fields']['city']['collect']) ? $data['city'] : $location['city'];
        $location['province'] = empty($location['province']) || empty($location['location_settings']['form']['fields']['province']['collect']) ? $data['province'] : $location['province'];
      }
    }
  }

  // Normalize province.
  // Note: Validation is performed elsewhere. We assume that the province
  // specified matches either the short or long form of a province.
  if (!empty($location['province']) && !empty($location['country'])) {
    $location['province'] = location_province_code($location['country'], $location['province']);
  }

  // @@@ Now would be a GREAT time to hook.
}