diff options
author | Struan Donald <struan@exo.org.uk> | 2013-01-17 13:01:24 +0000 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2013-01-17 13:01:24 +0000 |
commit | 00d711fc3cb01050d69862e1c7dce5ff8dee6e34 (patch) | |
tree | cf841ef009eb74b108813b5ddc6f45d0edd8fab6 | |
parent | b570f6531bb33141add046ce68e3c25491ebad60 (diff) |
add an ajax location lookup method for doing string -> lat,long lookup
factor out shared code with autocomplete method to save repetition
-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; |