aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Geocode/Google.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/Geocode/Google.pm')
-rw-r--r--perllib/FixMyStreet/Geocode/Google.pm43
1 files changed, 9 insertions, 34 deletions
diff --git a/perllib/FixMyStreet/Geocode/Google.pm b/perllib/FixMyStreet/Geocode/Google.pm
index 11ff8ef80..8d3280ae7 100644
--- a/perllib/FixMyStreet/Geocode/Google.pm
+++ b/perllib/FixMyStreet/Geocode/Google.pm
@@ -7,11 +7,6 @@
package FixMyStreet::Geocode::Google;
use strict;
-use Encode;
-use File::Slurp;
-use File::Path ();
-use LWP::Simple;
-use Digest::MD5 qw(md5_hex);
use mySociety::Locale;
# string STRING CONTEXT
@@ -24,6 +19,13 @@ sub string {
my $params = $c->cobrand->disambiguate_location($s);
+ # For some reason adding gl=uk is no longer sufficient to make google
+ # think we are in the UK for some locations so we explictly add UK to
+ # the address.
+ if ($c->cobrand->country eq 'GB' && $s !~ /, *UK/ && $s !~ /united *kingdom$/) {
+ $s .= ', UK';
+ }
+
$s = FixMyStreet::Geocode::escape($s);
my $url = 'http://maps.google.com/maps/geo?q=' . $s;
@@ -36,38 +38,11 @@ sub string {
}
$url .= '&hl=' . $params->{lang} if $params->{lang};
- my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'google/';
- my $cache_file = $cache_dir . md5_hex($url);
- my $js;
- if (-s $cache_file) {
- $js = File::Slurp::read_file($cache_file);
- } else {
- # For some reason adding gl=uk is no longer sufficient to make google
- # think we are in the UK for some locations so we explictly add UK to
- # the address. We do it here so as not to invalidate existing cache
- # entries
- if ( $c->cobrand->country eq 'GB'
- && $url !~ /,\+UK/
- && $url !~ /united\++kingdom$/ )
- {
- if ( $url =~ /&/ ) {
- $url =~ s/&/,+UK&/;
- } else {
- $url .= ',+UK';
- }
- }
- $url .= '&sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY');
- $js = LWP::Simple::get($url);
- $js = encode_utf8($js) if utf8::is_utf8($js);
- File::Path::mkpath($cache_dir);
- File::Slurp::write_file($cache_file, $js) if $js && $js !~ /"code":6[12]0/;
- }
-
+ my $args = 'sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY');
+ my $js = FixMyStreet::Geocode::cache('google', $url, $args, qr/"code":6[12]0/);
if (!$js) {
return { error => _('Sorry, we could not parse that location. Please try again.') };
}
-
- $js = JSON->new->utf8->allow_nonref->decode($js);
if ($js->{Status}->{code} ne '200') {
return { error => _('Sorry, we could not find that location.') };
}