_location_geo_logic

  1. drupal
    1. 6 location.module function
    2. 7 location.module function
Drupal 7 _location_geo_logic(&$location, $changed, $filled, $inhibit_geocode = FALSE)

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

2 calls to _location_geo_logic()

File

sites/all/modules/location/location.module, line 1472
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 (  !empty($changed['street']) || !empty($changed['additional']) || 
          !empty($changed['city']) || !empty($changed['province']) || 
          !empty($changed['country']) || !empty($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?
      }
      // Attempt inexact geocoding against a local postcode database
      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.

}