function google_geocode_location in Location 7.3
Same name and namespace in other branches
- 5.3 geocoding/google.inc \google_geocode_location()
- 5 geocoding/google.inc \google_geocode_location()
- 6.3 geocoding/google.inc \google_geocode_location()
- 7.5 geocoding/google.inc \google_geocode_location()
- 7.4 geocoding/google.inc \google_geocode_location()
Perform a geocode on a location array.
Parameters
array $location: The location array to process.
Return value
array an associative array with keys 'lat' and 'lon' containing the coordinates.
1 call to google_geocode_location()
- la_geocode_worker in contrib/
location_autofill/ la.module - Handler to process single location per batch run.
File
- geocoding/
google.inc, line 40 - Google geocoder.
Code
function google_geocode_location($location = array()) {
$delay_trigger =& drupal_static(__FUNCTION__);
$delay = variable_get('location_geocode_google_delay', 0);
if ($delay > 0 && $delay_trigger) {
usleep($delay * 1000);
}
$query = array(
'region' => $location['country'],
'address' => _google_geocode_flatten($location),
// Required by TOS.
'sensor' => 'false',
);
if (!empty($location['country'])) {
// If we have only a country code and nothing else,
// pass in country component so that google doesn't mistake the country
// code for some other short code, like administrative area.
// @codingStandardsIgnoreStart
$filtered_location = array_filter($location, function ($a) {
// Handle locpick & settings arrays.
// No actual location data should be in an array.
if (is_array($a)) {
return FALSE;
}
return is_string($a) ? trim($a) : $a;
});
// @codingStandardsIgnoreEnd
if (count($filtered_location) == 1) {
$query['components'] = _google_geocode_get_components($filtered_location);
}
}
$key = variable_get('location_geocode_google_apikey', '');
if (empty($key) && function_exists('gmap_get_key')) {
$key = gmap_get_key();
}
if (!empty($key)) {
$query['key'] = $key;
}
$url = url('https://maps.googleapis.com/maps/api/geocode/json', array(
'query' => $query,
'external' => TRUE,
));
$http_reply = drupal_http_request($url);
$delay_trigger = TRUE;
$data = json_decode($http_reply->data);
$status_code = $data->status;
if ($status_code != 'OK') {
watchdog('location', 'Google geocoding returned status code: %status_code for the query url: %url', array(
'%status_code' => $data->status,
'%url' => $url,
));
return NULL;
}
$location = $data->results[0]->geometry->location;
return array(
'lat' => $location->lat,
'lon' => $location->lng,
);
}