diff options
author | Matthew Somerville <matthew@mysociety.org> | 2012-10-12 15:52:23 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2012-10-12 15:52:23 +0100 |
commit | ff6b2a8205a113310f8ab628043d19691c759dbc (patch) | |
tree | 5c8737d79d7d751f72a61bec6b53f4e652661918 /perllib/FixMyStreet | |
parent | 32613ed491f1b414c0a49f85d8eeaf5f88dd95b4 (diff) |
Allow lat,lon and OS grid references in search box, fixes #142.
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Location.pm | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Location.pm b/perllib/FixMyStreet/App/Controller/Location.pm index c3d754485..3e44cd748 100644 --- a/perllib/FixMyStreet/App/Controller/Location.pm +++ b/perllib/FixMyStreet/App/Controller/Location.pm @@ -64,6 +64,19 @@ sub determine_location_from_pc : Private { $pc ||= $c->req->param('pc') || return; $c->stash->{pc} = $pc; # for template + if ( $pc =~ /^(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)$/ ) { + $c->stash->{latitude} = $1; + $c->stash->{longitude} = $2; + return $c->forward( 'check_location' ); + } + if ( $c->cobrand->country eq 'GB' && $pc =~ /^([A-Z])([A-Z])([\d\s]+)$/i ) { + if (my $convert = gridref_to_latlon( $1, $2, $3 )) { + $c->stash->{latitude} = $convert->{latitude}; + $c->stash->{longitude} = $convert->{longitude}; + return $c->forward( 'check_location' ); + } + } + my ( $latitude, $longitude, $error ) = FixMyStreet::Geocode::lookup( $pc, $c ); @@ -114,6 +127,36 @@ sub check_location : Private { return 1; } +# Utility function for if someone (rarely) enters a grid reference +sub gridref_to_latlon { + my ( $a, $b, $num ) = @_; + $a = ord(uc $a) - 65; $a-- if $a > 7; + $b = ord(uc $b) - 65; $b-- if $b > 7; + my $e = (($a-2)%5)*5 + $b%5; + my $n = 19 - int($a/5)*5 - int($b/5); + + $num =~ s/\s+//g; + my $l = length($num); + return if $l % 2 or $l > 10; + + $l /= 2; + $e .= substr($num, 0, $l); + $n .= substr($num, $l); + + if ( $l < 5 ) { + $e .= 5; + $n .= 5; + $e .= 0 x (4-$l); + $n .= 0 x (4-$l); + } + + my ( $lat, $lon ) = Utils::convert_en_to_latlon( $e, $n ); + return { + latitude => $lat, + longitude => $lon, + }; +} + =head1 AUTHOR Struan Donald |