Skip to content

Instantly share code, notes, and snippets.

@perifer
Created March 1, 2011 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perifer/849145 to your computer and use it in GitHub Desktop.
Save perifer/849145 to your computer and use it in GitHub Desktop.
Converts addresses from a node and saves them as simple_geo positions, using the Google Geocoding API.
<?php
/**
* Converts addresses from a node and saves them as simple_geo positions, using
* the Google Geocoding API: http://code.google.com/apis/maps/documentation/geocoding/
*/
// Add this to a hook_menu:
// $items['admin/simple-geo/geocode-example'] = array(
// 'title' => 'Simple geo geocoding',
// 'page callback' => 'simple_geo_geocode',
// 'access arguments' => array('administer simple geo'),
// 'file' => 'includes/simple_geo_geocode_example.inc',
// 'type' => MENU_CALLBACK,
// );
function simple_geo_geocode() {
// TODO: Use batch API
// TODO: Basic UI
$api_url = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=';
// Get the nodes we want to get latitude and longitude for.
$sql = 'SELECT DISTINCT(node.nid) AS nid
FROM {node}
INNER JOIN content_field_publish_year {node_data_field_publish_year} ON node.vid = node_data_field_publish_year.vid
LEFT JOIN simple_geo_position {simple_geo_position} ON node.nid = simple_geo_position.nid AND simple_geo_position.type = "node"
WHERE (node.type IN ("restaurant")) AND (node_data_field_publish_year.field_publish_year_value = 2011)
AND (AsText(simple_geo_position.position) = "POINT(0 0)" OR simple_geo_position.position IS NULL)';
$result = db_query($sql);
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
$address = trim($node->field_address[0]['value']);
$city = trim($node->field_city[0]['value']);
$postal_code = trim($node->field_postalcode[0]['value']);
$full_address = $address . ' ' . $postal_code . ' ' . $city;
// Do the geocoding.
$url = $api_url . urlencode($full_address);
$api_result = drupal_http_request($url);
$data = json_decode($api_result->data);
if (empty($data->results[0]->geometry->location)) {
$request = var_export($api_result, TRUE);
print '<div style="color:red;">Could not save location for ' . l($node->title, 'node/' . $node->nid) . "</div>\n";
error_log("No location for $node->title ($node->nid). Request was $request");
}
else {
$data = json_decode($api_result->data);
$location = $data->results[0]->geometry->location;
$lat = $location->lat;
$lng = $location->lng;
// Save location for node.
simple_geo_set_position($node->nid, 'node', $lat . ' ' . $lng);
if ($data->results[0]->partial_match) {
print '<div style="color:orange;">Saved location for ' . l($node->title, 'node/' . $node->nid) . " (partial match)</div>\n";
}
else {
print '<div style="color:green;">Saved location for ' . l($node->title, 'node/' . $node->nid) . "</div>\n";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment