From 89094faf57a61dc65deb269e0e58ae30893bbfe3 Mon Sep 17 00:00:00 2001 From: Jerenus Date: Thu, 14 Mar 2013 18:23:22 +0800 Subject: [PATCH] Issue #1940474 by Jerenus: geocode update to v3. --- geocoding/google.inc | 43 ++++++++++++------------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/geocoding/google.inc b/geocoding/google.inc index 7115915..66d5132 100644 --- a/geocoding/google.inc +++ b/geocoding/google.inc @@ -125,46 +125,27 @@ function google_geocode_location($location = array()) { } $query = array( - 'key' => $key, - 'sensor' => 'false', // Required by TOS. - 'output' => 'xml', - //'ll' => 0, - //'spn' => 0, 'gl' => $location['country'], - 'q' => _google_geocode_flatten($location), + 'address' => _google_geocode_flatten($location), + 'sensor' => 'false', // Required by TOS. ); - $url = url('http://maps.google.com/maps/geo', array( + $url = url('http://maps.googleapis.com/maps/api/geocode/json', array( 'query' => $query, 'external' => TRUE, )); $http_reply = drupal_http_request($url); - $delay_trigger = TRUE; - - $status_code_match = array(); - preg_match('/(.*)<\/code>/', $http_reply->data, $status_code_match); - $status_code = $status_code_match[1]; - if ($status_code != 200) { - watchdog('location', 'Google geocoding returned status code: %status_code', array('%status_code' => $status_code)); - return NULL; - } + $data = json_decode($http_reply->data); - $accuracy_code_match = array(); - preg_match('/Accuracy="([0-9])"/', $http_reply->data, $accuracy_code_match); - $accuracy_code = $accuracy_code_match[1]; - $min_accuracy = variable_get('location_geocode_' . $location['country'] . 'google_accuracy_code', variable_get('location_geocode_google_minimum_accuracy', '3')); - if ($accuracy_code < $min_accuracy) { - watchdog('location', 'Google geocoding result for %country did not meet the minimum accuracy level of %min_accuracy. Result accuracy: %accuracy_code', array('%country' => $location['country'], '%min_accuracy' => $min_accuracy, '%accuracy_code' => $accuracy_code)); + $status_code = $data->status; + if ($status_code != 'OK') { + watchdog('location', 'Google geocoding returned status code: %status_code', array('%status_code' => $data->status)); return NULL; } + $location = $data->results[0]->geometry->location; - $latlon_match = array(); - preg_match('/(.*)<\/coordinates>/', $http_reply->data, $latlon_match); - - $latlon_exploded = explode(',', $latlon_match[1]); - - return array('lat' => $latlon_exploded[1], 'lon' => $latlon_exploded[0]); + return array('lat' => $location->lat, 'lon' => $location->lng); } /** @@ -230,7 +211,7 @@ function _google_geocode_flatten($location = array()) { if (!empty($location['city'])) { if (!empty($address)) { - $address .= ', '; + $address .= ' '; } $address .= $location['city']; @@ -238,7 +219,7 @@ function _google_geocode_flatten($location = array()) { if (!empty($location['province'])) { if (!empty($address)) { - $address .= ', '; + $address .= ' '; } // @@@ Fix this! @@ -260,7 +241,7 @@ function _google_geocode_flatten($location = array()) { if (!empty($location['country'])) { if (!empty($address)) { - $address .= ', '; + $address .= ' '; } $address .= $location['country']; } -- 1.7.10.4