diff --git a/geocoding/google.inc b/geocoding/google.inc index 8f5d5bf..94e21a4 100644 --- a/geocoding/google.inc +++ b/geocoding/google.inc @@ -111,53 +111,28 @@ function google_geocode_info() { */ function google_geocode_location($location = array()) { - if (function_exists('gmap_get_key')) { - $key = gmap_get_key(); - } - else { - $key = variable_get('location_geocode_google_apikey', ''); - } - $query = array( - 'key' => $key, + 'region' => $location['country'], + 'address' => _google_geocode_flatten($location), 'sensor' => 'false', // Required by TOS. - 'output' => 'xml', - //'ll' => 0, - //'spn' => 0, - 'gl' => $location['country'], - 'q' => _google_geocode_flatten($location), ); - $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); + $data = json_decode($http_reply->data); - $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)); + $status_code = $data->status; + if ($status_code != 'OK') { + watchdog('location', 'Google geocoding returned status code: %status_code for the query url: %url', array('%status_code' => $data->status, '%url' => $url)); return NULL; } + $location = $data->results[0]->geometry->location; - $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]); + return array('lat' => $location->lat, 'lon' => $location->lng); } /** @@ -238,7 +213,7 @@ function _google_geocode_flatten($location = array()) { if (!empty($location['postal_code'])) { if (!empty($address)) { - $address .= ' '; + $address .= ', '; } $address .= $location['postal_code']; } diff --git a/tests/google_geocoder.test b/tests/google_geocoder.test index a37441d..4afefc8 100644 --- a/tests/google_geocoder.test +++ b/tests/google_geocoder.test @@ -79,7 +79,7 @@ class LocationGoogleGeocoderTest extends LocationTestCase { // @todo fixme //$this->assertEqual($location['source'], LOCATION_LATLON_GEOCODED_EXACT); - $expected = array(37.421972, -122.084143); + $expected = array(37.4218378, -122.0846263); $result = array($location['latitude'], $location['longitude']); // @todo fixme //$this->assertArrayEpsilon($result, $expected, 0.01, 'Google Headquarters');