aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2011-06-30 16:56:12 +0100
committerStruan Donald <struan@exo.org.uk>2011-06-30 16:56:12 +0100
commit0e6bb3e802c0614d64366bd19732b37a574dc12c (patch)
tree01b5d111754a0acef3f8a71da526ce1fbe59de55 /perllib/FixMyStreet
parentb756440e4b5c0b356ee0d65d1647124317535c95 (diff)
parentfb78afe61194ea9b6fbec9596d69627e315ab97f (diff)
Merge branch 'master' of ssh://git.mysociety.org/data/git/public/fixmystreet into new_statuses
Conflicts: perllib/FixMyStreet/App/Controller/Reports.pm
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App.pm6
-rw-r--r--perllib/FixMyStreet/App/Controller/Around.pm23
-rw-r--r--perllib/FixMyStreet/App/Controller/Auth.pm6
-rwxr-xr-xperllib/FixMyStreet/App/Controller/Questionnaire.pm23
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm4
-rw-r--r--perllib/FixMyStreet/App/Controller/Reports.pm69
-rw-r--r--perllib/FixMyStreet/App/Controller/Tokens.pm14
-rw-r--r--perllib/FixMyStreet/Cobrand/Barnet.pm8
-rw-r--r--perllib/FixMyStreet/Cobrand/Default.pm9
-rw-r--r--perllib/FixMyStreet/Cobrand/FiksGataMi.pm7
-rw-r--r--perllib/FixMyStreet/Cobrand/Southampton.pm8
-rw-r--r--perllib/FixMyStreet/Geocode.pm159
-rw-r--r--perllib/FixMyStreet/Map.pm25
-rw-r--r--perllib/FixMyStreet/Map/OSM.pm26
-rw-r--r--perllib/FixMyStreet/Map/Tilma/Original.pm10
15 files changed, 268 insertions, 129 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm
index 47b290eff..90f1f24bc 100644
--- a/perllib/FixMyStreet/App.pm
+++ b/perllib/FixMyStreet/App.pm
@@ -51,8 +51,7 @@ __PACKAGE__->config(
'Plugin::Session' => { # Catalyst::Plugin::Session::Store::DBIC
dbic_class => 'DB::Session',
- expires => 3600 * 24 * 7 * 6, # 6 months
- cookie_expires => 0
+ expires => 3600 * 24 * 7 * 4, # 4 weeks
},
'Plugin::Authentication' => {
@@ -181,7 +180,8 @@ sub setup_request {
Memcached::set_namespace( FixMyStreet->config('BCI_DB_NAME') . ":" );
- FixMyStreet::Map::set_map_class( $c->request->param('map') );
+ my $map = $host =~ /^osm\./ ? 'OSM' : $c->req->param('map');
+ FixMyStreet::Map::set_map_class( $map );
return $c;
}
diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm
index ec1fde4e4..4ca559fad 100644
--- a/perllib/FixMyStreet/App/Controller/Around.pm
+++ b/perllib/FixMyStreet/App/Controller/Around.pm
@@ -196,6 +196,7 @@ sub display_location : Private {
} @$on_map_all, @$around_map;
}
+ $c->stash->{page} = 'around'; # So the map knows to make clickable pins, update on pan
FixMyStreet::Map::display_map(
$c,
latitude => $latitude,
@@ -234,31 +235,19 @@ the map.
sub ajax : Path('/ajax') {
my ( $self, $c ) = @_;
- # Our current X/Y middle of visible map
- my $x = ( $c->req->param('x') || 0 ) + 0;
- my $y = ( $c->req->param('y') || 0 ) + 0;
-
- # Where we started as that's the (0,0) we have to work to
- my $sx = ( $c->req->param('sx') || 0 ) + 0;
- my $sy = ( $c->req->param('sy') || 0 ) + 0;
-
# how far back should we go?
my $all_pins = $c->req->param('all_pins') ? 1 : undef;
my $interval = $all_pins ? undef : $c->cobrand->on_map_default_max_pin_age;
# extract the data from the map
my ( $pins, $on_map, $around_map, $dist ) =
- FixMyStreet::Map::map_pins( $c, $x, $y, $sx, $sy, $interval );
+ FixMyStreet::Map::map_pins( $c, $interval );
# render templates to get the html
- # my $on_map_list_html = $c->forward(
- # "View::Web", "render",
my $on_map_list_html =
$c->view('Web')
->render( $c, 'around/on_map_list_items.html', { on_map => $on_map } );
- # my $around_map_list_html = $c->forward(
- # "View::Web", "render",
my $around_map_list_html = $c->view('Web')->render(
$c,
'around/around_map_list_items.html',
@@ -278,8 +267,12 @@ sub ajax : Path('/ajax') {
$c->res->content_type('text/javascript; charset=utf-8');
$c->res->header( 'Cache_Control' => 'max-age=0' );
- # Set the body - note that the js needs the surrounding brackets.
- $c->res->body("($body)");
+ if ( $c->req->param('bbox') ) {
+ $c->res->body($body);
+ } else {
+ # The JS needs the surrounding brackets for Tilma
+ $c->res->body("($body)");
+ }
}
__PACKAGE__->meta->make_immutable;
diff --git a/perllib/FixMyStreet/App/Controller/Auth.pm b/perllib/FixMyStreet/App/Controller/Auth.pm
index 8aed746ec..ebb3510d0 100644
--- a/perllib/FixMyStreet/App/Controller/Auth.pm
+++ b/perllib/FixMyStreet/App/Controller/Auth.pm
@@ -71,7 +71,11 @@ sub sign_in : Private {
return 1;
}
- $c->stash->{sign_in_error} = 1;
+ $c->stash(
+ sign_in_error => 1,
+ email => $email,
+ remember_me => $remember_me,
+ );
return;
}
diff --git a/perllib/FixMyStreet/App/Controller/Questionnaire.pm b/perllib/FixMyStreet/App/Controller/Questionnaire.pm
index a146b85ce..90674b294 100755
--- a/perllib/FixMyStreet/App/Controller/Questionnaire.pm
+++ b/perllib/FixMyStreet/App/Controller/Questionnaire.pm
@@ -19,24 +19,17 @@ Deals with report questionnaires.
=cut
-=head2 load_questionnaire
+=head2 check_questionnaire
-Loads the questionnaire from the database, and checks it still needs answering
-and is in the right state. Also finds out if this user has answered the
-"ever reported" question before.
+Checks the questionnaire still needs answering and is in the right state. Also
+finds out if this user has answered the "ever reported" question before.
=cut
-sub load_questionnaire : Private {
+sub check_questionnaire : Private {
my ( $self, $c ) = @_;
- my $questionnaire = $c->model('DB::Questionnaire')->find(
- { id => $c->stash->{id} },
- { prefetch => 'problem' }
- );
- $c->detach('missing_problem') unless $questionnaire;
-
- $c->stash->{questionnaire} = $questionnaire;
+ my $questionnaire = $c->stash->{questionnaire};
my $problem_id = $questionnaire->problem_id;
@@ -155,8 +148,8 @@ sub submit_creator_fixed : Private {
sub submit_standard : Private {
my ( $self, $c ) = @_;
- $c->forward( '/tokens/load_questionnaire_id', [ $c->req->params->{token} ] );
- $c->forward( 'load_questionnaire' );
+ $c->forward( '/tokens/load_questionnaire', [ $c->req->params->{token} ] );
+ $c->forward( 'check_questionnaire' );
$c->forward( 'process_questionnaire' );
my $problem = $c->stash->{problem};
@@ -270,7 +263,7 @@ sub process_questionnaire : Private {
# Sent here from email token action. Simply load and display questionnaire.
sub index : Private {
my ( $self, $c ) = @_;
- $c->forward( 'load_questionnaire' );
+ $c->forward( 'check_questionnaire' );
$c->forward( 'display' );
}
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index 4488ce8cd..3a7d18a1c 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -71,10 +71,6 @@ phone
partial
-=head2 can be ignored
-
-all_pins: related to map display - not relevant to creation of a new report
-
=cut
sub report_new : Path : Args(0) {
diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm
index fc8c4cd0e..9cec0fdfe 100644
--- a/perllib/FixMyStreet/App/Controller/Reports.pm
+++ b/perllib/FixMyStreet/App/Controller/Reports.pm
@@ -93,10 +93,36 @@ sub ward : Path : Args(2) {
$c->forward( 'load_and_group_problems' );
$c->forward( 'sort_problems' );
- $c->stash->{rss_url} = '/rss/reports/'
- . $c->cobrand->short_name( $c->stash->{council}, $c->stash->{areas_info} );
+ my $council_short = $c->cobrand->short_name( $c->stash->{council}, $c->stash->{areas_info} );
+ $c->stash->{rss_url} = '/rss/reports/' . $council_short;
$c->stash->{rss_url} .= '/' . $c->cobrand->short_name( $c->stash->{ward} )
if $c->stash->{ward};
+
+ $c->stash->{council_url} = '/reports/' . $council_short;
+
+ my $pins = $c->stash->{pins};
+
+ # Even though front end doesn't yet have it, have it on this page, it's better!
+ FixMyStreet::Map::set_map_class( 'FMS' );
+ FixMyStreet::Map::display_map(
+ $c,
+ latitude => @$pins ? $pins->[0]{latitude} : 0,
+ longitude => @$pins ? $pins->[0]{longitude} : 0,
+ area => $c->stash->{ward} ? $c->stash->{ward}->{id} : $c->stash->{council}->{id},
+ pins => $pins,
+ any_zoom => 1,
+ );
+
+ # List of wards
+ unless ($c->stash->{ward}) {
+ my $children = mySociety::MaPit::call('area/children', $c->stash->{council}->{id} );
+ foreach (values %$children) {
+ $_->{url} = $c->uri_for( $c->stash->{council_url}
+ . '/' . $c->cobrand->short_name( $_ )
+ );
+ }
+ $c->stash->{children} = $children;
+ }
}
sub rss_council : Regex('^rss/(reports|area)$') : Args(1) {
@@ -258,41 +284,41 @@ sub load_parent : Private {
sub load_and_group_problems : Private {
my ( $self, $c ) = @_;
+ my $page = $c->req->params->{p} || 1;
+
my $where = {
state => [ FixMyStreet::DB::Result::Problem->visible_states() ]
};
- my @extra_cols = ();
if ($c->stash->{ward}) {
$where->{areas} = { 'like', '%' . $c->stash->{ward}->{id} . '%' }; # FIXME Check this is secure
- push @extra_cols, 'title', 'detail';
} elsif ($c->stash->{council}) {
$where->{areas} = { 'like', '%' . $c->stash->{council}->{id} . '%' };
- push @extra_cols, 'title', 'detail';
}
my $problems = $c->cobrand->problems->search(
$where,
{
columns => [
- 'id', 'council', 'state', 'areas',
+ 'id', 'council', 'state', 'areas', 'latitude', 'longitude', 'title',
{ duration => { extract => "epoch from current_timestamp-lastupdate" } },
{ age => { extract => "epoch from current_timestamp-confirmed" } },
- @extra_cols,
],
- order_by => { -desc => 'id' },
+ order_by => { -desc => 'lastupdate' },
+ rows => 100,
}
- );
+ )->page( $page );
+ $c->stash->{pager} = $problems->pager;
$problems = $problems->cursor; # Raw DB cursor for speed
- my ( %fixed, %open );
+ my ( %fixed, %open, @pins );
my $re_councils = join('|', keys %{$c->stash->{areas_info}});
- my @cols = ( 'id', 'council', 'state', 'areas', 'duration', 'age', 'title', 'detail' );
+ my @cols = ( 'id', 'council', 'state', 'areas', 'latitude', 'longitude', 'title', 'duration', 'age' );
while ( my @problem = $problems->next ) {
my %problem = zip @cols, @problem;
if ( !$problem{council} ) {
# Problem was not sent to any council, add to possible councils
$problem{councils} = 0;
while ($problem{areas} =~ /,($re_councils)(?=,)/g) {
- add_row( \%problem, $1, \%fixed, \%open );
+ add_row( \%problem, $1, \%fixed, \%open, \@pins );
}
} else {
# Add to councils it was sent to
@@ -301,13 +327,16 @@ sub load_and_group_problems : Private {
$problem{councils} = scalar @council;
foreach ( @council ) {
next if $c->stash->{council} && $_ != $c->stash->{council}->{id};
- add_row( \%problem, $_, \%fixed, \%open );
+ add_row( \%problem, $_, \%fixed, \%open, \@pins );
}
}
}
- $c->stash->{fixed} = \%fixed;
- $c->stash->{open} = \%open;
+ $c->stash(
+ fixed => \%fixed,
+ open => \%open,
+ pins => \@pins,
+ );
return 1;
}
@@ -348,7 +377,7 @@ sub redirect_area : Private {
my $fourweeks = 4*7*24*60*60;
sub add_row {
- my ( $problem, $council, $fixed, $open ) = @_;
+ my ( $problem, $council, $fixed, $open, $pins ) = @_;
my $duration_str = ( $problem->{duration} > 2 * $fourweeks ) ? 'old' : 'new';
my $type = ( $problem->{duration} > 2 * $fourweeks )
? 'unknown'
@@ -359,6 +388,14 @@ sub add_row {
# Open problems are either unknown, older, or new
push @{$open->{$council}{$type}}, $problem if
exists FixMyStreet::DB::Result::Problem->open_states->{$problem->{state}};
+
+ push @$pins, {
+ latitude => $problem->{latitude},
+ longitude => $problem->{longitude},
+ colour => $problem->{state} eq 'fixed' ? 'green' : 'red',
+ id => $problem->{id},
+ title => $problem->{title},
+ };
}
=head1 AUTHOR
diff --git a/perllib/FixMyStreet/App/Controller/Tokens.pm b/perllib/FixMyStreet/App/Controller/Tokens.pm
index c9c9f3ab7..1fef0f07e 100644
--- a/perllib/FixMyStreet/App/Controller/Tokens.pm
+++ b/perllib/FixMyStreet/App/Controller/Tokens.pm
@@ -154,7 +154,7 @@ sub confirm_update : Path('/C') {
return 1;
}
-sub load_questionnaire_id : Private {
+sub load_questionnaire : Private {
my ( $self, $c, $token_code ) = @_;
# Set up error handling
@@ -164,11 +164,21 @@ sub load_questionnaire_id : Private {
my $auth_token = $c->forward( 'load_auth_token', [ $token_code, 'questionnaire' ] );
$c->stash->{id} = $auth_token->data;
$c->stash->{token} = $token_code;
+
+ my $questionnaire = $c->model('DB::Questionnaire')->find(
+ { id => $c->stash->{id} },
+ { prefetch => 'problem' }
+ );
+ $c->detach('/questionnaire/missing_problem') unless $questionnaire;
+ $c->stash->{questionnaire} = $questionnaire;
}
sub questionnaire : Path('/Q') : Args(1) {
my ( $self, $c, $token_code ) = @_;
- $c->forward( 'load_questionnaire_id', [ $token_code ] );
+ $c->forward( 'load_questionnaire', [ $token_code ] );
+
+ $c->authenticate( { email => $c->stash->{questionnaire}->problem->user->email }, 'no_password' );
+ $c->set_session_cookie_expire(0);
$c->forward( '/questionnaire/index');
}
diff --git a/perllib/FixMyStreet/Cobrand/Barnet.pm b/perllib/FixMyStreet/Cobrand/Barnet.pm
index bccd27885..9496570ea 100644
--- a/perllib/FixMyStreet/Cobrand/Barnet.pm
+++ b/perllib/FixMyStreet/Cobrand/Barnet.pm
@@ -63,9 +63,11 @@ sub all_councils_report {
}
sub disambiguate_location {
- my ( $self, $s ) = @_;
- $s = "ll=51.612832,-0.218169&spn=0.0563,0.09&$s";
- return $s;
+ return {
+ centre => '51.612832,-0.218169',
+ span => '0.0563,0.09',
+ bounds => [ '51.584682,-0.263169', '51.640982,-0.173169' ],
+ };
}
sub recent_photos {
diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm
index d8cd426ba..29061ebd8 100644
--- a/perllib/FixMyStreet/Cobrand/Default.pm
+++ b/perllib/FixMyStreet/Cobrand/Default.pm
@@ -303,12 +303,15 @@ sub front_stats_data {
=head2 disambiguate_location
-Given a STRING ($_[1]) representing a location and a QUERY, return a string that
-includes any disambiguating information available
+Returns disambiguating information available
=cut
-sub disambiguate_location { "$_[1]&gl=uk" }
+sub disambiguate_location {
+ return {
+ country => 'uk',
+ };
+}
=head2 form_elements
diff --git a/perllib/FixMyStreet/Cobrand/FiksGataMi.pm b/perllib/FixMyStreet/Cobrand/FiksGataMi.pm
index 29d2ec867..3d855cf8e 100644
--- a/perllib/FixMyStreet/Cobrand/FiksGataMi.pm
+++ b/perllib/FixMyStreet/Cobrand/FiksGataMi.pm
@@ -28,9 +28,10 @@ sub enter_postcode_text {
# Is also adding language parameter
sub disambiguate_location {
- my ( $self, $s ) = @_;
- $s = "hl=no&gl=no&$s";
- return $s;
+ return {
+ lang => 'no',
+ country => 'no',
+ };
}
sub area_types {
diff --git a/perllib/FixMyStreet/Cobrand/Southampton.pm b/perllib/FixMyStreet/Cobrand/Southampton.pm
index 6b4aae742..abf6e3082 100644
--- a/perllib/FixMyStreet/Cobrand/Southampton.pm
+++ b/perllib/FixMyStreet/Cobrand/Southampton.pm
@@ -63,9 +63,11 @@ sub all_councils_report {
}
sub disambiguate_location {
- my ( $self, $s ) = @_;
- $s = "ll=50.913822,-1.400493&spn=0.084628,0.15701&$s";
- return $s;
+ return {
+ centre => '50.913822,-1.400493',
+ span => '0.084628,0.15701',
+ bounds => [ '50.871508,-1.478998', '50.956136,-1.321988' ],
+ };
}
sub recent_photos {
diff --git a/perllib/FixMyStreet/Geocode.pm b/perllib/FixMyStreet/Geocode.pm
index d5cc7f1d3..286a10105 100644
--- a/perllib/FixMyStreet/Geocode.pm
+++ b/perllib/FixMyStreet/Geocode.pm
@@ -16,21 +16,15 @@ use LWP::Simple;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
-use mySociety::Config;
use mySociety::Locale;
use mySociety::MaPit;
use mySociety::PostcodeUtil;
use mySociety::Web qw(NewURL);
-BEGIN {
- (my $dir = __FILE__) =~ s{/[^/]*?$}{};
- mySociety::Config::set_file("$dir/../../conf/general");
-}
-
-# lookup STRING QUERY
+# lookup STRING CONTEXT
# Given a user-inputted string, try and convert it into co-ordinates using either
# MaPit if it's a postcode, or Google Maps API otherwise. Returns an array of
-# data, including an error if there is one (which includes a location being in
+# data, including an error if there is one (which includes a location being in
# Northern Ireland). The information in the query may be used by cobranded versions
# of the site to diambiguate locations.
sub lookup {
@@ -58,57 +52,49 @@ sub lookup {
unless ($error || defined $latitude) {
($latitude, $longitude, $error) = FixMyStreet::Geocode::string($s, $c);
}
- return ($latitude, $longitude, $error);
-}
-
-sub geocoded_string_coordinates {
- my ( $c, $js ) = @_;
- my ($latitude, $longitude, $error);
- my ($accuracy) = $js =~ /"Accuracy" *: *(\d)/;
- if ($accuracy < 4) {
- $error = _('Sorry, that location appears to be too general; please be more specific.');
- } elsif ( $js =~ /"coordinates" *: *\[ *(.*?), *(.*?),/ ) {
- $longitude = $1;
- $latitude = $2;
+ unless ($error || defined $latitude) {
+ $error = _('Sorry, we could not find that location.');
}
return ($latitude, $longitude, $error);
}
-sub results_check {
- my $c = shift;
- my ($error, @valid_locations);
- foreach (@_) {
- next unless /"address" *: *"(.*?)"/s;
- my $address = $1;
- next unless $c->cobrand->geocoded_string_check( $address );
- next if $address =~ /BT\d/;
- push (@$error, $address);
- push (@valid_locations, $_);
- }
- if (scalar @valid_locations == 1) {
- return geocoded_string_coordinates( $c, $valid_locations[0] );
- }
- $error = _('Sorry, we could not find that location.') unless $error;
- return (undef, undef, $error);
-}
-
-# string STRING QUERY
-# Canonicalises, looks up on Google Maps API, and caches, 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.
+# string STRING CONTEXT
+# Canonicalises, and then passes to some external API to look stuff up.
sub string {
my ($s, $c) = @_;
$s = lc($s);
$s =~ s/[^-&\w ']/ /g;
$s =~ s/\s+/ /g;
$s = URI::Escape::uri_escape_utf8($s);
- $s = $c->cobrand->disambiguate_location( "q=$s" );
$s =~ s/%20/+/g;
- my $url = 'http://maps.google.com/maps/geo?' . $s;
- my $cache_dir = mySociety::Config::get('GEO_CACHE');
+ my $params = $c->cobrand->disambiguate_location();
+ if ( FixMyStreet->config('BING_MAPS_API_KEY') ) {
+ my $lookup = FixMyStreet::Geocode::string_bing($s, $c, $params);
+ return ( $lookup->{latitude}, $lookup->{longitude}, $lookup->{error} );
+ }
+ if ( FixMyStreet->config('GOOGLE_MAPS_API_KEY') ) {
+ my $lookup = FixMyStreet::Geocode::string_google($s, $c, $params);
+ return ( $lookup->{latitude}, $lookup->{longitude}, $lookup->{error} );
+ }
+}
+
+# string_google STRING CONTEXT
+# Looks up on Google Maps API, and caches, 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.
+sub string_google {
+ my ( $s, $c, $params ) = @_;
+
+ my $url = 'http://maps.google.com/maps/geo?q=' . $s;
+ $url .= '&ll=' . $params->{centre} if $params->{centre};
+ $url .= '&spn=' . $params->{span} if $params->{span};
+ $url .= '&gl=' . $params->{country} if $params->{country};
+ $url .= '&hl=' . $params->{lang} if $params->{lang};
+
+ my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'google/';
my $cache_file = $cache_dir . md5_hex($url);
- my ($js, $error);
+ my $js;
if (-s $cache_file) {
$js = File::Slurp::read_file($cache_file);
} else {
@@ -126,25 +112,86 @@ sub string {
$url .= ',+UK';
}
}
- $url .= '&sensor=false&key=' . mySociety::Config::get('GOOGLE_MAPS_API_KEY');
+ $url .= '&sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY');
$js = LWP::Simple::get($url);
$js = encode_utf8($js) if utf8::is_utf8($js);
File::Path::mkpath($cache_dir);
File::Slurp::write_file($cache_file, $js) if $js && $js !~ /"code":6[12]0/;
}
+
if (!$js) {
- $error = _('Sorry, we could not parse that location. Please try again.');
- } elsif ($js !~ /"code" *: *200/) {
- $error = _('Sorry, we could not find that location.');
- } elsif ($js =~ /}, *{/) { # Multiple
- return results_check($c, (split /}, *{/, $js));
+ return { error => _('Sorry, we could not parse that location. Please try again.') };
} elsif ($js =~ /BT\d/) {
# Northern Ireland, hopefully
- $error = _("We do not cover Northern Ireland, I'm afraid, as our licence doesn't include any maps for the region.");
+ return { error => _("We do not cover Northern Ireland, I'm afraid, as our licence doesn't include any maps for the region.") };
+ }
+
+ $js = JSON->new->utf8->allow_nonref->decode($js);
+ if ($js->{Status}->{code} ne '200') {
+ return { error => _('Sorry, we could not find that location.') };
+ }
+
+ my $results = $js->{Placemark};
+ my ( $error, @valid_locations, $latitude, $longitude );
+ foreach (@$results) {
+ next unless $_->{AddressDetails}->{Accuracy} >= 4;
+ my $address = $_->{address};
+ next unless $c->cobrand->geocoded_string_check( $address );
+ ( $longitude, $latitude ) = @{ $_->{Point}->{coordinates} };
+ push (@$error, $address);
+ push (@valid_locations, $_);
+ }
+ return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1;
+ return { error => $error };
+}
+
+# string_bing STRING CONTEXT
+# Looks up on Bing Maps API, and caches, 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.
+sub string_bing {
+ my ( $s, $c, $params ) = @_;
+ my $url = "http://dev.virtualearth.net/REST/v1/Locations?q=$s&c=en-GB"; # FIXME nb-NO for Norway
+ $url .= '&mapView=' . $params->{bounds}[0] . ',' . $params->{bounds}[1]
+ if $params->{bounds};
+ $url .= '&userLocation=' . $params->{centre} if $params->{centre};
+
+ my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/';
+ my $cache_file = $cache_dir . md5_hex($url);
+ my $js;
+ if (-s $cache_file) {
+ $js = File::Slurp::read_file($cache_file);
} else {
- return results_check($c, $js);
+ $url .= '&key=' . FixMyStreet->config('BING_MAPS_API_KEY');
+ $js = LWP::Simple::get($url);
+ $js = encode_utf8($js) if utf8::is_utf8($js);
+ File::Path::mkpath($cache_dir);
+ File::Slurp::write_file($cache_file, $js) if $js;
+ }
+
+ if (!$js) {
+ return { error => _('Sorry, we could not parse that location. Please try again.') };
+ } elsif ($js =~ /BT\d/) {
+ return { error => _("We do not cover Northern Ireland, I'm afraid, as our licence doesn't include any maps for the region.") };
+ }
+
+ $js = JSON->new->utf8->allow_nonref->decode($js);
+ if ($js->{statusCode} ne '200') {
+ return { error => _('Sorry, we could not find that location.') };
+ }
+
+ my $results = $js->{resourceSets}->[0]->{resources};
+ my ( $error, @valid_locations, $latitude, $longitude );
+ foreach (@$results) {
+ my $address = $_->{name};
+ next unless $_->{address}->{countryRegion} eq 'United Kingdom'; # FIXME This is UK only
+ ( $latitude, $longitude ) = @{ $_->{point}->{coordinates} };
+ push (@$error, $address);
+ push (@valid_locations, $_);
}
- return (undef, undef, $error);
+ return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1;
+ return { error => $error };
}
sub mapit_check_error {
diff --git a/perllib/FixMyStreet/Map.pm b/perllib/FixMyStreet/Map.pm
index 125aca9e6..6b5a811a6 100644
--- a/perllib/FixMyStreet/Map.pm
+++ b/perllib/FixMyStreet/Map.pm
@@ -68,12 +68,29 @@ sub map_features {
# use deltas that are roughly 500m in the UK - so we get a 1 sq km search box
my $lat_delta = 0.00438;
my $lon_delta = 0.00736;
+ return _map_features(
+ $c, $lat, $lon,
+ $lon - $lon_delta, $lat - $lat_delta,
+ $lon + $lon_delta, $lat + $lat_delta,
+ $interval
+ );
+}
+
+sub map_features_bounds {
+ my ( $c, $min_lon, $min_lat, $max_lon, $max_lat, $interval ) = @_;
- my $min_lat = $lat - $lat_delta;
- my $max_lat = $lat + $lat_delta;
+ my $lat = ( $max_lat + $min_lat ) / 2;
+ my $lon = ( $max_lon + $min_lon ) / 2;
+ return _map_features(
+ $c, $lat, $lon,
+ $min_lon, $min_lat,
+ $max_lon, $max_lat,
+ $interval
+ );
+}
- my $min_lon = $lon - $lon_delta;
- my $max_lon = $lon + $lon_delta;
+sub _map_features {
+ my ( $c, $lat, $lon, $min_lon, $min_lat, $max_lon, $max_lat, $interval ) = @_;
# list of problems around map can be limited, but should show all pins
my $around_limit = $c->cobrand->on_map_list_limit || undef;
diff --git a/perllib/FixMyStreet/Map/OSM.pm b/perllib/FixMyStreet/Map/OSM.pm
index 9b968b4f6..05dc6ad39 100644
--- a/perllib/FixMyStreet/Map/OSM.pm
+++ b/perllib/FixMyStreet/Map/OSM.pm
@@ -95,6 +95,32 @@ sub display_map {
};
}
+sub map_pins {
+ my ($self, $c, $interval) = @_;
+
+ my $bbox = $c->req->param('bbox');
+ my ( $min_lon, $min_lat, $max_lon, $max_lat ) = split /,/, $bbox;
+
+ my ( $around_map, $around_map_list, $nearby, $dist ) =
+ FixMyStreet::Map::map_features_bounds( $c, $min_lon, $min_lat, $max_lon, $max_lat, $interval );
+
+ # create a list of all the pins
+ my @pins = map {
+ # Here we might have a DB::Problem or a DB::Nearby, we always want the problem.
+ my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_;
+ #{
+ # latitude => $p->latitude,
+ # longitude => $p->longitude,
+ # colour => $p->state eq 'fixed' ? 'green' : 'red',
+ # id => $p->id,
+ # title => $p->title,
+ #}
+ [ $p->latitude, $p->longitude, $p->state eq 'fixed' ? 'green' : 'red', $p->id, $p->title ]
+ } @$around_map, @$nearby;
+
+ return (\@pins, $around_map_list, $nearby, $dist);
+}
+
sub compass {
my ( $x, $y, $z ) = @_;
return {
diff --git a/perllib/FixMyStreet/Map/Tilma/Original.pm b/perllib/FixMyStreet/Map/Tilma/Original.pm
index 8fd835265..46cf3ad40 100644
--- a/perllib/FixMyStreet/Map/Tilma/Original.pm
+++ b/perllib/FixMyStreet/Map/Tilma/Original.pm
@@ -96,7 +96,15 @@ sub display_pin {
}
sub map_pins {
- my ($self, $c, $x, $y, $sx, $sy, $interval) = @_;
+ my ($self, $c, $interval) = @_;
+
+ # Our current X/Y middle of visible map
+ my $x = ( $c->req->param('x') || 0 ) + 0;
+ my $y = ( $c->req->param('y') || 0 ) + 0;
+
+ # Where we started as that's the (0,0) we have to work to
+ my $sx = ( $c->req->param('sx') || 0 ) + 0;
+ my $sy = ( $c->req->param('sy') || 0 ) + 0;
my $e = tile_to_os($x);
my $n = tile_to_os($y);