You are here

function google_geocode_location in Location 5

Same name and namespace in other branches
  1. 5.3 geocoding/google.inc \google_geocode_location()
  2. 6.3 geocoding/google.inc \google_geocode_location()
  3. 7.5 geocoding/google.inc \google_geocode_location()
  4. 7.3 geocoding/google.inc \google_geocode_location()
  5. 7.4 geocoding/google.inc \google_geocode_location()

File

geocoding/google.inc, line 49

Code

function google_geocode_location($location = array()) {
  switch ($location['country']) {
    case 'li':
      $location['country'] = 'Liechtenstein';
      $query = location_address2singleline($location);
      break;
    case 'at':
      $location['country'] = 'Austria';
      $query = location_address2singleline($location);
      break;
    case 'uk':
      if ($location['street'] && $location['city']) {
        unset($location['postal_code']);
      }
      elseif ($location['postal_code']) {
        unset($location['city']);
        unset($location['street']);
        unset($location['additional']);
      }
      else {
        unset($location['postal_code']);
      }
      $query = location_address2singleline($location);
      break;
    default:
      $query = location_address2singleline($location);
      break;
  }
  $service_url = 'http://maps.google.com/maps/geo?output=xml&key=' . variable_get('location_geocode_google_apikey', '') . '&q=';
  $http_reply = drupal_http_request($service_url . urlencode($query));
  $status_code_match = array();
  preg_match('/<code>(.*)<\\/code>/', $http_reply->data, $status_code_match);
  $status_code = $status_code_match[1];
  if ($status_code != 200) {
    return NULL;
  }
  $accuracy_code_match = array();
  preg_match('/Accuracy="([0-9])"/', $http_reply->data, $accuracy_code_match);
  $accuracy_code = $accuracy_code_match[1];
  if ($accuracy_code < 3) {
    return NULL;
  }
  $latlon_match = array();
  preg_match('/<coordinates>(.*)<\\/coordinates>/', $http_reply->data, $latlon_match);
  $latlon_exploded = explode(',', $latlon_match[1]);
  return array(
    'lat' => $latlon_exploded[1],
    'lon' => $latlon_exploded[0],
  );
}