You are here

function location_geocode_ca_geocoder in Location 5

Same name and namespace in other branches
  1. 5.3 supported/location.ca.inc \location_geocode_ca_geocoder()
  2. 6.3 supported/location.ca.inc \location_geocode_ca_geocoder()
  3. 7.5 supported/location.ca.inc \location_geocode_ca_geocoder()
  4. 7.3 supported/location.ca.inc \location_geocode_ca_geocoder()
  5. 7.4 supported/location.ca.inc \location_geocode_ca_geocoder()

This needs some more work to cover errors and such. I think showing a proper Drupal error is a good idea. If an incorrect address is given to geocoder.ca it offers a suggestion. If this happens the drupal user should be told.

File

supported/location.ca.inc, line 306

Code

function location_geocode_ca_geocoder($location = array()) {
  $service_url = "http://geocoder.ca/?geoit=XML";
  if (variable_get('location_geocode_ca_geocoder_apikey', NULL)) {
    $service_url .= '&auth=' . variable_get('location_geocode_ca_geocoder_apikey', NULL) . '&locate=';
  }
  else {
    $service_url .= '&locate=';
  }

  // geocoder gives better results when not usong postal code, country and ,
  unset($location['postal_code'], $location['country']);
  $address = strtr(location_address2singleline($location), array(
    ', ' => ' ',
  ));
  $http_reply = drupal_http_request($service_url . urlencode($address));
  if ($http_reply->code == 400) {
    return NULL;
  }
  else {
    $matches = array();
    $lat_match = array();
    $lon_match = array();
    $latlon = array();
    if (preg_match('/<error>(.*)<\\/error>/', $http_reply->data, $lat_match)) {
      return NULL;
    }
    if (preg_match('/<latt>(.*)<\\/latt>/', $http_reply->data, $lat_match)) {
      $latlon['lat'] = $lat_match[1];
    }
    else {
      return NULL;
    }
    if (preg_match('/<longt>(.*)<\\/longt>/', $http_reply->data, $lon_match)) {
      $latlon['lon'] = $lon_match[1];
      return $latlon;
    }
    else {
      return NULL;
    }
  }
}