Merge "Refactor geo location lookup"

This commit is contained in:
Jenkins 2014-09-12 17:04:23 +00:00 committed by Gerrit Code Review
commit 460e685507
2 changed files with 54 additions and 30 deletions

View File

@ -152,29 +152,15 @@ function field_group_location_field_widget_form(&$form, &$form_state, $field,
return $element + $widget;
}
/**
* Implements hook_field_presave()
*
* Lookup continent data by country name.
*/
function field_group_location_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
module_load_include('inc', 'field_group_location', 'field_group_lookup');
$continent_lookup = _country_by_continent_predefined_list();
foreach ($items as $delta => &$item) {
$item['continent'] = $continent_lookup[$item['country']];
// lookup long, lat values
$query = array(
'address' => $item['location'],
'components' => 'country:'.$item['country'],
'sensor' => 'false',
);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?' . drupal_http_build_query($query);
$response = drupal_http_request($url);
if (empty($response->error)) {
$data = json_decode($response->data);
if ($data->status == 'OK') {
foreach ($data->results as $result) {
if (!empty($result->formatted_address)) {
$item['lat'] = $result->geometry->location->lat;
$item['lng'] = $result->geometry->location->lng;
}
}
}
}
}
}

View File

@ -15,25 +15,63 @@ function groups_groups_node_prepare($node) {
}
}
/**
* Lookup latitude and longitude coordinates by location and country
* using Google's geocode API.
*
* @param string $location Location (eg. Los Angeles, CA)
* @param string $country Country of the location
* @return array lat, lng coordinates
*/
function _groups_groups_lookup_lat_lng($location, $country) {
$query = array(
'components' => 'country:'.$country,
'sensor' => 'false',
);
if (isset($location)) {
$query['address'] = $location;
}
watchdog('Groups groups', 'Lookup geolocation data [query=@query]',
array('@query' => print_r($query, TRUE)), WATCHDOG_DEBUG);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?' . drupal_http_build_query($query);
$response = drupal_http_request($url);
if (empty($response->error)) {
$data = json_decode($response->data);
if ($data->status == 'OK') {
foreach ($data->results as $result) {
if (!empty($result->formatted_address)) {
watchdog('Groups groups', 'Lookup geolocation data result @lat;@lng [query=@query]',
array('@query' => print_r($query, TRUE),
'@lat' => $result->geometry->location->lat,
'@lng' => $result->geometry->location->lng), WATCHDOG_DEBUG);
return array($result->geometry->location->lat, $result->geometry->location->lng);
}
}
}
}
throw new Exception(t('Failed to lookup geolocation data [query=@query].', array('@query' => print_r($query, TRUE))));
}
/**
* Implements hook node presave
*
* Update geofield from group location values.
* Update geofield from group location values. Override lat / long values
* when field_geofield is empty.
*/
function groups_groups_node_presave($node) {
if ($node->type == 'group') {
if ((isset($node->field_group_location['und'][0]['lat']) &&
(isset($node->field_group_location['und'][0]['lng'])))) {
if (($node->type == 'group') && (empty($node->field_geofield[LANGUAGE_NONE]))) {
try {
geophp_load();
$latitude = $node->field_group_location['und'][0]['lat'];
$longitude = $node->field_group_location['und'][0]['lng'];
$geom = new Point($longitude, $latitude);
list($lat, $lng) = _groups_groups_lookup_lat_lng(
$node->field_group_location['und'][0]['location'],
$node->field_group_location['und'][0]['country']);
$geom = new Point($lng, $lat);
$geofield = geofield_get_values_from_geometry($geom);
$geofield['geom'] = 'POINT (' . $longitude . ' ' . $latitude . ')';
$geofield['geom'] = 'POINT (' . $lng . ' ' . $lat . ')';
$node->field_geofield = array('und'=>array($geofield));
} else {
$node->field_geofield = array();
} catch (Exception $e) {
watchdog('Groups groups', 'Node presave failed: @err',
array('@err' => $e->getMessage()), WATCHDOG_ERROR);
}
}
}