warn Dumper($content) if $DEBUG;
- if ( $content->{result}->{addressMatches} ) {
+ my $addressMatches_ref = $content->{result}->{addressMatches};
- my $tract = $content->{result}->{addressMatches}[0]->{geographies}->{'Census Blocks'}[0]->{GEOID};
+ if ( $addressMatches_ref && scalar @{$addressMatches_ref} ) {
+
+ my $tract = $addressMatches_ref->[0]->{geographies}->{'Census Blocks'}[0]->{GEOID};
return $tract;
} else {
<BR>
% my $querystring = "census_year=$year&address=$location{address1}, $location{address2}, $location{city}, $location{state}";
<A HREF="<%$p%>misc/openmap.html?<% $querystring %>"
- TARGET="_blank">Map service module location</A><BR>
+ REL="opener"
+ TARGET="_blank"
+>Map service module location</A><BR>
% $querystring = "census_year=$year&pre=$pre&zip_code=" . $cache->get('zip');
<A HREF="<%$p%>misc/openmap.html?<% $querystring %>"
- TARGET="_blank">Map zip code center</A><BR>
+ REL="opener"
+ TARGET="_blank"
+>Map zip code center</A><BR>
<BR>
<input type="hidden" id="new_tract" name="new_tract" value="<%$new_tract%>">
<TABLE>
function getCensusTract(lat, lon) {
var url = 'xmlhttp-censustract.html?lat=' + lat + '&lon=' + lon + '&census_year=<%$census_year%>';
$.getJSON(url,function(data){
- var tract = (data.Block.FIPS.substr(0, 11) / 100).toFixed(2);
- document.getElementById("mycensustract").innerHTML = tract;
+ document.getElementById("mycensustract").innerHTML = data.Block.FIPS;
});
}
my $address = $cgi->param('address');
my $loc = $zip_code ? $zip_code : $address;
-</%init>
\ No newline at end of file
+</%init>
my $conf = new FS::Conf;
-my $return = {};
-
## new api link, see doc https://geo.fcc.gov/api/census/
my $url = "https://geo.fcc.gov/api/census/block/find?format=json&censusYear=" . $cgi->param('census_year') . "&latitude=" . $cgi->param('lat') . "&longitude=" . $cgi->param('lon');
-use LWP::Simple;
-my $return = get $url;
+my $ua = new LWP::UserAgent;
+$ua->agent('Freeside/'. $FS::VERSION); #libwww* elicits "403 Forbidden"
+my $res = $ua->get($url);
+
+my $return = '';
+if ( $res->is_success ) {
+ $return = $res->decoded_content;
+} else {
+ #better error handling? well, hopefully the site is reliable enough
+ warn 'Error from geo.fcc.gov: '. $res->status_line. "\n";
+}
-</%init>
\ No newline at end of file
+</%init>