diff options
author | Matthew Somerville <matthew@mysociety.org> | 2013-01-07 17:34:44 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2013-01-07 17:34:44 +0000 |
commit | 37ae388673a4898426ac2ea502b14ab2b6ca18e3 (patch) | |
tree | 7635b827942765564cba4a188171abcd83969034 /perllib/FixMyStreet/App/Controller/Around.pm | |
parent | 7981e2acd299846bd4aede377d4ade54e1a0fac3 (diff) |
Autocomplete of Zurich street names (2beb264 plus tweaks).
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Around.pm')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index 200dbd696..5090ef7ff 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -281,6 +281,33 @@ sub ajax : Path('/ajax') { $c->res->body($body); } + +sub location_autocomplete : Path('/ajax/geocode') { + my ( $self, $c ) = @_; + $c->res->content_type('application/json; charset=utf-8'); + unless ( $c->req->param('term') ) { + $c->res->status(404); + $c->res->body(''); + return; + } + # we want the match even if there's no ambiguity, so recommendation doesn't + # disappear when it's the last choice being offered in the autocomplete. + $c->stash->{allow_single_geocode_match_strings} = 1; + my ( $lat, $long, $suggestions ) = + FixMyStreet::Geocode::lookup( $c->req->param('term'), $c ); + my @addresses; + # $error doubles up to return multiple choices by being an array + if ( ref($suggestions) eq 'ARRAY' ) { + foreach (@$suggestions) { + push @addresses, decode_utf8($_->{address}); + } + } + my $body = JSON->new->utf8(1)->encode( + \@addresses + ); + $c->res->body($body); +} + __PACKAGE__->meta->make_immutable; 1; |