function tools_geocode_data() {
$result = db_query("SELECT * FROM {data} WHERE (lat = 0 OR lat IS NULL)");
while ($data = db_fetch_object($result)) {
$node = new stdClass();
$node->field_location_street[0]['value'] = $data->address . " " . $data->city;
$node->field_location_postal[0]['value'] = $data->zipcode1 . $data->zipcode2;
$location = tools_google_geocode_location($node);
//drupal_set_message("GEOCODE: " . $result['lat'] . " - " . $result['lon']);
if ($location['lat']) {
$cnt += 1;
db_query("UPDATE {justeatimport} SET lat = %f, lon = %f WHERE id = %d", $location['lat'], $location['lon'], $data->id);
usleep(250000);
}
}
drupal_set_message("Updated $cnt businesses.");
}
function tools_geocode_location($node) {
$key = "INSERT YOUR API KEY HERE";
$query = array(
'key' => $key,
'sensor' => 'false', // Required by TOS.
'output' => 'xml',
//'ll' => 0,
//'spn' => 0,
'gl' => 'CA',
'q' => tools_address_flatten($node),
);
$url = url('http://maps.google.com/maps/geo', array(
'query' => $query,
'external' => TRUE,
));
//drupal_set_message("GOOGLE: " . $url);
$http_reply = drupal_http_request($url);
$status_code_match = array();
preg_match('/(.*)/', $http_reply->data, $status_code_match);
$status_code = $status_code_match[1];
if ($status_code != 200) {
return NULL;
}
$accuracy_code_match = array();
preg_match('/Accuracy="([0-9])"/', $http_reply->data, $accuracy_code_match);
$accuracy_code = $accuracy_code_match[1];
if ($accuracy_code
return NULL;
}
$latlon_match = array();
preg_match('/(.*)/', $http_reply->data, $latlon_match);
//drupal_set_message("lat/lon: " . $latlon_match[1], 'status');
$latlon_exploded = explode(',', $latlon_match[1]);
return array('lat' => $latlon_exploded[1], 'lon' => $latlon_exploded[0]);
}
function tools_address_flatten($node) {
// Check if its a valid address
if (empty($node)) {
return '';
}
if ($node->nid) {
$term = taxonomy_node_get_terms_by_vocabulary($node, 1, 'vid');
$city = $term[1]->name;
$province = $term[1]->description;
}
$address = '';
if (!empty($node->field_location_street[0]['value'])) {
$address .= $node->field_location_street[0]['value'];
}
if (!empty($city)) {
if (!empty($node->field_location_street[0]['value'])) {
$address .= ', ';
}
$address .= $city;
}
if (!empty($province)) {
if (!empty($node->field_location_street[0]['value']) || !empty($node->field_location_city[0]['value'])) {
$address .= ', ';
}
$address .= $province;
}
if (!empty($node->field_location_postal[0]['value'])) {
if (!empty($address)) {
$address .= ' ';
}
$address .= $node->field_location_postal[0]['value'];
}
return $address;
}
?>