Skip to content

Instantly share code, notes, and snippets.

@mrded
Created June 26, 2014 14:15
Show Gist options
  • Save mrded/e051b21b1a18d231eaef to your computer and use it in GitHub Desktop.
Save mrded/e051b21b1a18d231eaef to your computer and use it in GitHub Desktop.
Drupal 7: Get location object by address string.
<?php
/**
* Get location object by address string.
*
* @param string $location
*
* @return array
*
* @dependencies geocoder module.
*/
function wj_users_fboauth_get_location($location = '') {
$point = geocoder('google', $location);
$output = array();
foreach ($point->data['geocoder_address_components'] as $component) {
if ($component->types[0] == 'locality') {
$output['city'] = array(
'name' => $component->long_name,
);
continue;
}
if ($component->types[0] == 'country') {
$output['country'] = array(
'name' => $component->long_name,
'iso2' => $component->short_name,
);
continue;
}
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment