aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Cobrand/Default.pm
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2017-12-22 12:53:48 +0000
committerMatthew Somerville <matthew-github@dracos.co.uk>2018-05-09 12:57:56 +0100
commit22f0fed0b1ce6e7effac37e025bd55dc6043a838 (patch)
treea4bd66224c560418863319e9a6144d9b8702d462 /perllib/FixMyStreet/Cobrand/Default.pm
parent22815e994510659870987609d244b3f6324bab22 (diff)
ajax endpoint to return closest address.
/ajax/closest will return ajax with details of the closest address to the lat/lon passed in from the Bing geocoder. Tidy up find_closest() to use overloaded string rather than passing in whether you want a string or not.
Diffstat (limited to 'perllib/FixMyStreet/Cobrand/Default.pm')
-rw-r--r--perllib/FixMyStreet/Cobrand/Default.pm35
1 files changed, 17 insertions, 18 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm
index 99b247412..92494250e 100644
--- a/perllib/FixMyStreet/Cobrand/Default.pm
+++ b/perllib/FixMyStreet/Cobrand/Default.pm
@@ -5,6 +5,7 @@ use strict;
use warnings;
use FixMyStreet;
use FixMyStreet::DB;
+use FixMyStreet::Geocode::Address;
use FixMyStreet::Geocode::Bing;
use DateTime;
use List::MoreUtils 'none';
@@ -514,34 +515,32 @@ sub geocoded_string_check { return 1; }
=item find_closest
Used by send-reports and similar to attach nearest things to the bottom of the
-report.
+report. This can be called with either a hash of lat/lon or a Problem.
=cut
sub find_closest {
- my ( $self, $problem, $as_data ) = @_;
+ my ($self, $data) = @_;
+ $data = { problem => $data } if ref $data ne 'HASH';
+
+ my $problem = $data->{problem};
+ my $lat = $problem ? $problem->latitude : $data->{latitude};
+ my $lon = $problem ? $problem->longitude : $data->{longitude};
+ my $j = $problem->geocode if $problem;
- my $j = $problem->geocode;
if (!$j) {
- $j = FixMyStreet::Geocode::Bing::reverse( $problem->latitude, $problem->longitude,
+ $j = FixMyStreet::Geocode::Bing::reverse( $lat, $lon,
disambiguate_location()->{bing_culture} );
- # cache the bing results for use in alerts
- $problem->geocode( $j );
- $problem->update;
- }
-
- my $data = $as_data ? {} : '';
- if ($j && $j->{resourceSets}[0]{resources}[0]{name}) {
- my $str = $j->{resourceSets}[0]{resources}[0]{name};
- if ($as_data) {
- $data->{road} = $str;
- } else {
- $data .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s"),
- $str) . "\n\n";
+ if ($problem) {
+ # cache the bing results for use in alerts
+ $problem->geocode( $j );
+ $problem->update;
}
}
- return $data;
+ return FixMyStreet::Geocode::Address->new($j->{resourceSets}[0]{resources}[0])
+ if $j && $j->{resourceSets}[0]{resources}[0]{name};
+ return {};
}
=item find_closest_address_for_rss