You are here

function getlocations_geocode in Get Locations 6

Same name and namespace in other branches
  1. 6.2 getlocations.module \getlocations_geocode()

Geocode an address Input can be a string or an array Output is an array if successful, string if failed

File

./getlocations.module, line 1739
Displays locations on a map. for Drupal 6 using version 3 googlemaps API

Code

function getlocations_geocode($address) {
  if (empty($address)) {
    return;
  }
  if (is_array($address)) {
    if (count($address)) {
      $address = implode('+', $address);
    }
    else {
      return;
    }
  }
  $query = array(
    'address' => $address,
    'sensor' => 'false',
  );
  $msg = '';
  $url = url("http://maps.googleapis.com/maps/api/geocode/json", array(
    'query' => $query,
  ));
  $result = drupal_http_request($url);
  $data = json_decode($result->data);
  $status = $data->status;
  if ($status == 'OK') {
    return array(
      'latitude' => $data->results[0]->geometry->location->lat,
      'longitude' => $data->results[0]->geometry->location->lng,
      'location_type' => $data->results[0]->geometry->location_type,
    );
  }
  elseif ($status == 'ZERO_RESULTS') {
    $msg = t("No result");
  }
  elseif ($status == 'OVER_QUERY_LIMIT') {
    $msg = t("Over query limit");
  }
  elseif ($status == 'REQUEST_DENIED') {
    $msg = t("Request denied");
  }
  elseif ($status == 'INVALID_REQUEST') {
    $msg = t("Request invalid");
  }
  return $msg;

  /**
   * "OK" indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
   * "ZERO_RESULTS" indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
   * "OVER_QUERY_LIMIT" indicates that you are over your quota.
   * "REQUEST_DENIED" indicates that your request was denied, generally because of lack of a sensor parameter.
   * "INVALID_REQUEST" generally indicates that the query (address or latlng) is missing.
   */
}