aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2015-08-28 18:46:10 +0100
committerMatthew Somerville <matthew@mysociety.org>2015-08-28 18:46:10 +0100
commita4b6ed0fd694de47af3d0e57f31a021bae333e22 (patch)
tree244b9bc8eb0fbe33027d8a38713dd45cb9111ee0
parent8ef9368785c1950ae0aea8c13584288d395662d6 (diff)
Upgrade Google geocoder to version 3 of the API.
-rw-r--r--conf/general.yml-example4
-rw-r--r--perllib/FixMyStreet/Geocode/Google.pm28
2 files changed, 14 insertions, 18 deletions
diff --git a/conf/general.yml-example b/conf/general.yml-example
index ee68b5cd6..f16d63c70 100644
--- a/conf/general.yml-example
+++ b/conf/general.yml-example
@@ -120,9 +120,7 @@ BING_MAPS_API_KEY: ''
# bing_culture: <culture code, see http://msdn.microsoft.com/en-us/library/hh441729.aspx>
# bing_country: <country name, only accept results that match this>
#
-# If using Google, you can use:
-# centre: "<lat>,<lon>"
-# span: "<lat span>,<lon span>"
+# If using Google, you can use bounds, plus:
# google_country: <.ccTLD to restrict results to>
# lang: <language for results>
#
diff --git a/perllib/FixMyStreet/Geocode/Google.pm b/perllib/FixMyStreet/Geocode/Google.pm
index 35fcec36f..5261bb7e4 100644
--- a/perllib/FixMyStreet/Geocode/Google.pm
+++ b/perllib/FixMyStreet/Geocode/Google.pm
@@ -28,34 +28,32 @@ sub string {
$s = FixMyStreet::Geocode::escape($s);
- my $url = 'http://maps.google.com/maps/geo?q=' . $s;
- $url .= '&ll=' . $params->{centre} if $params->{centre};
- $url .= '&spn=' . $params->{span} if $params->{span};
+ my $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $s;
+ $url .= '&bounds=' . $params->{bounds}[0] . ',' . $params->{bounds}[1]
+ . '|' . $params->{bounds}[2] . ',' . $params->{bounds}[3]
+ if $params->{bounds};
if ($params->{google_country}) {
- $url .= '&gl=' . $params->{google_country};
+ $url .= '&region=' . $params->{google_country};
} elsif ($params->{country}) {
- $url .= '&gl=' . $params->{country};
+ $url .= '&region=' . $params->{country};
}
- $url .= '&hl=' . $params->{lang} if $params->{lang};
+ $url .= '&language=' . $params->{lang} if $params->{lang};
- my $args = 'sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY');
- my $js = FixMyStreet::Geocode::cache('google', $url, $args, qr/"code":6[12]0/);
+ my $args = 'key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY');
+ my $js = FixMyStreet::Geocode::cache('google', $url, $args, qr/"status"\s*:\s*"(OVER_QUERY_LIMIT|REQUEST_DENIED|INVALID_REQUEST|UNKNOWN_ERROR)"/);
if (!$js) {
return { error => _('Sorry, we could not parse that location. Please try again.') };
}
- if ($js->{Status}->{code} ne '200') {
- return { error => _('Sorry, we could not find that location.') };
- }
+ return unless $js->{status} eq 'OK';
- my $results = $js->{Placemark};
+ my $results = $js->{results};
my ( $error, @valid_locations, $latitude, $longitude );
foreach (@$results) {
- next unless $_->{AddressDetails}->{Accuracy} >= 4;
- my $address = $_->{address};
+ my $address = $_->{formatted_address};
next unless $c->cobrand->geocoded_string_check( $address );
( $longitude, $latitude ) =
map { Utils::truncate_coordinate($_) }
- @{ $_->{Point}->{coordinates} };
+ ($_->{geometry}{location}{lat}, $_->{geometry}{location}{lng});
push (@$error, {
address => $address,
latitude => $latitude,