You are here

function ip_geoloc_set_location_from_address in IP Geolocation Views & Maps 7

Returns the supplied string into an array with various address components.

Parameters

string $partial_address_or_landmark:

Return value

array, a location holding, latitude, longitude and address fields

1 call to ip_geoloc_set_location_from_address()
_ip_geoloc_process_go_to_submit in ./ip_geoloc_blocks.inc
Submit handler to either go to a Region or "Move to" a street address.

File

./ip_geoloc_blocks.inc, line 847
Blocks available in IP Geolocation Views & Maps.

Code

function ip_geoloc_set_location_from_address($partial_address_or_landmark) {
  if (empty($partial_address_or_landmark)) {
    $location = array(
      'provider' => 'user',
      'ip_address' => ip_address(),
    );
  }
  else {
    if (module_exists('geocoder')) {

      // Use Google server-side API to retrieve lat/long from entered text, as
      // well as all components of the full address.
      $point = geocoder('google', $partial_address_or_landmark);
    }
    else {
      drupal_set_message(t('<a target="project_page" href="!geocoder">Geocoder</a> module must be enabled to geocode an address.', array(
        '!geocoder' => url('http://drupal.org/project/geocoder'),
      )), 'warning');
    }
    if (empty($point)) {
      drupal_set_message(t('The address %address could not be geocoded to a location.', array(
        '%address' => $partial_address_or_landmark,
      )), 'warning');
    }
    else {
      $location = array(
        'provider' => 'user+google',
        'is_updated' => TRUE,
        'ip_address' => ip_address(),
        'latitude' => $point->coords[1],
        'longitude' => $point->coords[0],
        'formatted_address' => $point->data['geocoder_formatted_address'],
        'accuracy' => $point->data['geocoder_accuracy'],
      );

      // Flatten the point object into a straight location array.
      foreach ($point->data['geocoder_address_components'] as $component) {
        if (!empty($component->long_name)) {
          $type = $component->types[0];
          $location[$type] = $component->long_name;
          if ($type == 'country' && !empty($component->short_name)) {
            $location['country_code'] = $component->short_name;
          }
        }
      }
      ip_geoloc_debug(t('IPGV&M: received from Geocoder (via Google) !location', array(
        '!location' => ip_geoloc_pretty_print($location),
      )));
    }
  }
  $location['fixed_address'] = 1;
  return $location;
}