You are here

public function IpGeoLocAPI::reverseGeocode in IP Geolocation Views & Maps 8

Uses the Google webservice to retrieve address information based on lat/long.

Effectively makes calls of this form: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=-37...

CALLER BEWARE: This is a server-side, as opposed to client-side call. If you want to call this function repeatedly, remember that Google imposes a limit of 2500 calls from the same IP address per 24 hours. It may return an OVER_QUERY_LIMIT response.

Parameters

string|double $latitude: E.g. "-37.87" or -37.87.

string|double $longitude: E.g. "144.98" or 144.98.

string $lang: Optional language specification as a two-letter code, e.g. 'ja' (Japanese).

Return value

array|bool Array of address components as received from Google or FALSE.

2 calls to IpGeoLocAPI::reverseGeocode()
IpGeoLocAPI::getAddress in src/Services/IpGeoLocAPI.php
Returns the formatted address reverse-geocoded from the supplied lat,long.
IpGeoLocAPI::getLocationByIp in src/Services/IpGeoLocAPI.php
Returns the location details associated with the supplied IP address.

File

src/Services/IpGeoLocAPI.php, line 514

Class

IpGeoLocAPI
Class IpGeoAPI to interact with other modules.

Namespace

Drupal\ip_geoloc\Services

Code

public function reverseGeocode($latitude, $longitude, $lang = NULL) {
  if (empty($latitude) || empty($latitude)) {
    drupal_set_message(t('IPGV&M: cannot reverse-geocode to address as no lat/long was specified.'), 'warning');
    return FALSE;
  }
  $query_start = microtime(TRUE);
  $url = IP_GEOLOC_GOOGLE_MAPS_SERVER . "?latlng={$latitude},{$longitude}";
  if (!empty($lang)) {
    $url .= "&language={$lang}";
  }
  $response = drupal_http_request($url);
  if (!empty($response->error)) {
    $msg_args = [
      '%url' => $url,
      '@code' => $response->code,
      '%error' => $response->error,
    ];
    drupal_set_message(t('IPGV&M: the HTTP request %url returned the following error (code @code): %error.', $msg_args), 'error');
    watchdog('IPGV&M', 'Error (code @code): %error. Request: %url', $msg_args, WATCHDOG_ERROR);
    return FALSE;
  }
  $data = drupal_json_decode($response->data);
  if ($data['status'] == 'OVER_QUERY_LIMIT') {
    $msg_args = [
      '%url' => $url,
    ];
    if (user_access('administer site configuration')) {
      drupal_set_message(t('IPGV&M: Server is over its query limit. Request: %url', $msg_args), 'error');
    }
    watchdog('IPGV&M', 'Server is over its query limit. Request: %url', $msg_args, WATCHDOG_ERROR);
    return FALSE;
  }
  if ($data['status'] == 'ZERO_RESULTS' || !isset($data['results'][0])) {
    $msg_args = [
      '@protocol' => $response->protocol,
      '%url' => $url,
    ];
    drupal_set_message(t('IPGV&M: the @protocol request %url succeeded, but returned no results.', $msg_args), 'warning');
    watchdog('IPGV&M', 'No results from @protocol request %url.', $msg_args, WATCHDOG_WARNING);
    return FALSE;
  }
  if ($data['status'] != 'OK') {
    $msg_args = [
      '%url' => $url,
      '%error' => $data['status'],
    ];
    drupal_set_message(t('IPGV&M: unknown error %error. Request: %url..', $msg_args), 'error');
    watchdog('IPGV&M', 'Unknown error %error. Request: %url.', $msg_args, WATCHDOG_ERROR);
    return FALSE;
  }
  $google_address = $data['results'][0];
  if (empty($google_address['formatted_address'])) {
    $msg_args = [
      '@lat' => $latitude,
      '@long' => $longitude,
    ];
    ip_geoloc_debug(t('IPGV&M: (@lat, @long) could not be reverse-geocoded to a street address.', $msg_args), 'warning');
    watchdog('IPGV&M', '(@lat, @long) could not be reverse-geocoded to a street address..', $msg_args, WATCHDOG_WARNING);
  }
  else {
    $sec = number_format(microtime(TRUE) - $query_start, 1);
    $msg_args = [
      '@lat' => $latitude,
      '@long' => $longitude,
      '%sec' => $sec,
      '%address' => $google_address['formatted_address'],
    ];
    ip_geoloc_debug(t('IPGV&M: %address reverse-geocoded from (@lat, @long) in %sec s.', $msg_args));
    watchdog('IPGV&M', '%address reverse-geocoded from (@lat, @long) in %sec s.', $msg_args, WATCHDOG_INFO);
  }
  return $google_address;
}