aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App/Controller/Around.pm27
-rw-r--r--perllib/FixMyStreet/Geocode/Zurich.pm14
2 files changed, 37 insertions, 4 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;
diff --git a/perllib/FixMyStreet/Geocode/Zurich.pm b/perllib/FixMyStreet/Geocode/Zurich.pm
index 5aaca2c8e..0fc84c0ef 100644
--- a/perllib/FixMyStreet/Geocode/Zurich.pm
+++ b/perllib/FixMyStreet/Geocode/Zurich.pm
@@ -52,8 +52,13 @@ sub setup_soap {
# string STRING CONTEXT
# Looks up on Zurich web service a user-inputted location.
# Returns array of (LAT, LON, ERROR), where ERROR is either undef, a string, or
-# an array of matches if there are more than one. The information in the query
-# may be used to disambiguate the location in cobranded versions of the site.
+# an array of matches if there are more than one.
+# If there is no ambiguity, returns only a {lat,long} hash, unless allow_single_match_string is true
+# (because the auto-complete use of this (in /around) should send the matched name even though it's not ambiguous).
+#
+# The information in the query may be used to disambiguate the location in cobranded
+# versions of the site.
+
sub string {
my ( $s, $c ) = @_;
@@ -98,8 +103,9 @@ sub string {
push (@valid_locations, $_);
last if lc($_->{text}) eq lc($s);
}
-
- return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1;
+ if (scalar @valid_locations == 1 && ! $c->stash->{allow_single_geocode_match_strings} ) {
+ return { latitude => $latitude, longitude => $longitude };
+ }
return { error => $error };
}