diff options
author | Struan Donald <struan@exo.org.uk> | 2012-05-23 17:39:00 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2012-05-23 17:39:00 +0100 |
commit | 7cb6bb21f713bc07a06ece5f4109cd6bd5a7f0b0 (patch) | |
tree | eaff3aa286d8e1910b33c204c79b6837e109a216 /perllib/FixMyStreet/Geocode/Bing.pm | |
parent | 9019fda388f9232181387e8cce1d28e8b89de1ee (diff) | |
parent | 3b0e39a4c89e4c184f30c6131936dc63845d6a1f (diff) |
Merge remote-tracking branch 'origin/master' into phonegap
Diffstat (limited to 'perllib/FixMyStreet/Geocode/Bing.pm')
-rw-r--r-- | perllib/FixMyStreet/Geocode/Bing.pm | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm index 856d7061e..a24f7c102 100644 --- a/perllib/FixMyStreet/Geocode/Bing.pm +++ b/perllib/FixMyStreet/Geocode/Bing.pm @@ -23,10 +23,11 @@ use Digest::MD5 qw(md5_hex); sub string { my ( $s, $c, $params ) = @_; $s .= '+' . $params->{town} if $params->{town} and $s !~ /$params->{town}/i; - 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] + my $url = "http://dev.virtualearth.net/REST/v1/Locations?q=$s"; + $url .= '&userMapView=' . $params->{bounds}[0] . ',' . $params->{bounds}[1] if $params->{bounds}; $url .= '&userLocation=' . $params->{centre} if $params->{centre}; + $url .= '&c=' . $params->{bing_culture} if $params->{bing_culture}; my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/'; my $cache_file = $cache_dir . md5_hex($url); @@ -43,7 +44,7 @@ sub string { if (!$js) { return { error => _('Sorry, we could not parse that location. Please try again.') }; - } elsif ($js =~ /BT\d/) { + } elsif ($js =~ /BT\d/ && $params->{bing_country} eq 'United Kingdom') { return { error => _("We do not currently cover Northern Ireland, I'm afraid.") }; } @@ -54,24 +55,43 @@ sub string { 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 + next unless $_->{address}->{countryRegion} eq $params->{bing_country}; + + # Getting duplicate, yet different, results from Bing sometimes + next if @valid_locations + && $_->{address}{postalCode} && $valid_locations[-1]{address}{postalCode} eq $_->{address}{postalCode} + && ( $valid_locations[-1]{address}{locality} eq $_->{address}{adminDistrict2} + || $valid_locations[-1]{address}{adminDistrict2} eq $_->{address}{locality} + || $valid_locations[-1]{address}{locality} eq $_->{address}{locality} + ); + ( $latitude, $longitude ) = @{ $_->{point}->{coordinates} }; - push (@$error, { address => $address, latitude => $latitude, longitude => $longitude }); + # These co-ordinates are output as query parameters in a URL, make sure they have a "." + mySociety::Locale::in_gb_locale { + push (@$error, { + address => $address, + latitude => sprintf('%0.6f', $latitude), + longitude => sprintf('%0.6f', $longitude) + }); + }; push (@valid_locations, $_); } + return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1; return { error => $error }; } sub reverse { - my ( $latitude, $longitude, $cache ) = @_; + my ( $latitude, $longitude, $bing_culture, $cache ) = @_; # Get nearest road-type thing from Bing my $key = mySociety::Config::get('BING_MAPS_API_KEY', ''); if ($key) { - my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?c=en-GB&key=$key"; + my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?key=$key"; + $url .= '&c=' . $bing_culture if $bing_culture; my $j; if ( $cache ) { my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/'; |