diff --git a/geocoding/google.inc b/geocoding/google.inc index 8f5d5bf..ef8a14d 100644 --- a/geocoding/google.inc +++ b/geocoding/google.inc @@ -119,45 +119,26 @@ 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', ); - $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); - $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)); + $data = json_decode($http_reply->data); + $status_code = $data->status; + if ($status_code != 'OK') { + watchdog('location', 'Google geocoding returned status code: %status_code', array('%status_code' => $data->status)); return NULL; } - - $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)); - return NULL; - } - - $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]); + $location = $data->results[0]->geometry->location; + return array('lat' => $location->lat, 'lon' => $location->lng); } /** @@ -215,7 +196,7 @@ function _google_geocode_flatten($location = array()) { if (!empty($location['city'])) { if (!empty($address)) { - $address .= ', '; + $address .= ' '; } $address .= $location['city']; @@ -223,7 +204,7 @@ function _google_geocode_flatten($location = array()) { if (!empty($location['province'])) { if (!empty($address)) { - $address .= ', '; + $address .= ' '; } // @@@ Fix this! @@ -245,7 +226,7 @@ function _google_geocode_flatten($location = array()) { if (!empty($location['country'])) { if (!empty($address)) { - $address .= ', '; + $address .= ' '; } $address .= $location['country']; }