function ip_geoloc_reverse_geocode in IP Geolocation Views & Maps 7
Same name and namespace in other branches
- 8 ip_geoloc_api.inc \ip_geoloc_reverse_geocode()
Uses the Google webservice to retrieve address information based on lat/long.
Effectively makes calls of this form: https://maps.googleapis.com/maps/api/geocode/json?latlng=-37.87,144.98
CALLER BEWARE: This is a server-side, as opposed to client-side call. If you want to call this function repeatedly, remember that Google 1) requires an API key (free) and 2) imposes other restrictions, like quotas and billing.
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.
3 calls to ip_geoloc_reverse_geocode()
- ip_geoloc_get_address in ./
ip_geoloc_api.inc - Returns the formatted address reverse-geocoded from the supplied lat,long.
- ip_geoloc_get_location_by_ip in ./
ip_geoloc_api.inc - Returns the location details associated with the supplied IP address.
- _ip_geoloc_process_access_log in ./
ip_geoloc.admin.inc - Callback for ip_geoloc_sync_with_accesslog batch process.
File
- ./
ip_geoloc_api.inc, line 499 - API functions of IP geolocation module
Code
function ip_geoloc_reverse_geocode($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 = array(
'%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 = array(
'%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 = array(
'@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 = array(
'%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 = array(
'@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 = array(
'@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;
}