diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Around.pm')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index 5090ef7ff..cb5bd1e82 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -293,6 +293,8 @@ sub location_autocomplete : Path('/ajax/geocode') { # 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; + return $self->_geocode( $c, $c->req->param('term') ); + my ( $lat, $long, $suggestions ) = FixMyStreet::Geocode::lookup( $c->req->param('term'), $c ); my @addresses; @@ -308,6 +310,50 @@ sub location_autocomplete : Path('/ajax/geocode') { $c->res->body($body); } +sub location_lookup : Path('/ajax/lookup_location') { + 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; + } + + return $self->_geocode( $c, $c->req->param('term') ); +} + +sub _geocode : Private { + my ( $self, $c, $term ) = @_; + + my ( $lat, $long, $suggestions ) = + FixMyStreet::Geocode::lookup( $c->req->param('term'), $c ); + + my ($response, @addresses); + + if ( $lat && $long ) { + $response = { latitude => $lat, longitude => $long }; + } else { + if ( ref($suggestions) eq 'ARRAY' ) { + foreach (@$suggestions) { + push @addresses, decode_utf8($_->{address}); + } + $response = { suggestions => \@addresses }; + } else { + $response = { error => $suggestions }; + } + } + + if ( $c->stash->{allow_single_geocode_match_strings} ) { + $response = \@addresses; + } + + my $body = JSON->new->utf8(1)->encode( + $response + ); + $c->res->body($body); + +} + __PACKAGE__->meta->make_immutable; 1; |