diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Around.pm')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index 5090ef7ff..8f651fae2 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -293,19 +293,51 @@ 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') ); +} + +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 @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 ($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( - \@addresses + $response ); $c->res->body($body); + } __PACKAGE__->meta->make_immutable; |