diff options
author | Christopher Burger <burgerc@freeside.biz> | 2018-11-16 09:09:13 -0500 |
---|---|---|
committer | Christopher Burger <burgerc@freeside.biz> | 2018-11-16 12:59:00 -0500 |
commit | 7bfcb1a73569815632cd7606c3d485234c21232e (patch) | |
tree | 1e14211e1e62e25115367a372efbac069c14ef3e | |
parent | d34861c4d32675dac5f0405f078adddb35d4c863 (diff) |
RT# 81730 - fixed XMLHttpRequest async issue, and fixed changed .gov api link.
-rw-r--r-- | httemplate/misc/openmap.html | 63 | ||||
-rw-r--r-- | httemplate/misc/xmlhttp-censustract.html | 4 |
2 files changed, 32 insertions, 35 deletions
diff --git a/httemplate/misc/openmap.html b/httemplate/misc/openmap.html index 73f107142..7cbe3db1d 100644 --- a/httemplate/misc/openmap.html +++ b/httemplate/misc/openmap.html @@ -11,50 +11,45 @@ <input type=hidden id="censustract" value=""> </td> <td> - <div id="mapid" style="width: 600px; height: 600px;"></div> + <div id="mapid" style="width: 600px; height: 600px;">Retrieving Map.</div> </td> </tr> </table> <script> - var mymap = L.map('mapid').setView(getLatLong('<%$loc%>'), 15); - var popup = L.popup(); - var mainLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { - maxZoom: 19, - attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' - }).addTo(mymap); - var markersLayer = new L.LayerGroup(); - mymap.addLayer(markersLayer); - function onMapClick(e) { - markersLayer.clearLayers(); - var marker = L.marker([e.latlng.lat, e.latlng.lng]); - marker.bindPopup("You selected (" + e.latlng.lat + " , " + e.latlng.lng + ")").openPopup(); - markersLayer.addLayer(marker); - getCensusTract(e.latlng.lat, e.latlng.lng); - } + var url = 'http://nominatim.openstreetmap.org/search?format=json&limit=1&q=<%$loc%>'; + $.getJSON(url,function(data){ + var latlong = [data[0].lat, data[0].lon]; + var mymap = L.map('mapid').setView(latlong, 15); + var popup = L.popup(); + var mainLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { + maxZoom: 19, + attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' + }).addTo(mymap); + var markersLayer = new L.LayerGroup(); + mymap.addLayer(markersLayer); - mymap.on('click', onMapClick); + function onMapClick(e) { + markersLayer.clearLayers(); + var marker = L.marker([e.latlng.lat, e.latlng.lng]); + marker.bindPopup("You selected (" + e.latlng.lat + " , " + e.latlng.lng + ")").openPopup(); + markersLayer.addLayer(marker); + document.getElementById("mycensustract").innerHTML = "Retrieving census tract..."; + getCensusTract(e.latlng.lat, e.latlng.lng); + } - function Get(yourUrl){ - var Httpreq = new XMLHttpRequest(); // a new request - Httpreq.open("GET",yourUrl,false); - Httpreq.send(); - return Httpreq.responseText; - } + mymap.on('click', onMapClick); - function getLatLong(loc) { - var url = 'http://nominatim.openstreetmap.org/search?format=json&limit=1&q=' + loc; - var json_obj = JSON.parse(Get(url)); - return [json_obj[0].lat, json_obj[0].lon]; - } + 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; + }); + } - function getCensusTract(lat, lon) { - var url = 'xmlhttp-censustract.html?lat=' + lat + '&lon=' + lon + '&census_year=<%$census_year%>'; - var json_obj = JSON.parse(Get(url)); - var tract = (json_obj.Block.FIPS.substr(0, 11) / 100).toFixed(2); - document.getElementById("mycensustract").innerHTML = tract; - } + }); function changeMainDiv(lat,lng) { window.opener.document.getElementById("newcensustract").innerHTML = document.getElementById("mycensustract").innerHTML; diff --git a/httemplate/misc/xmlhttp-censustract.html b/httemplate/misc/xmlhttp-censustract.html index 8a7686047..985fb90e5 100644 --- a/httemplate/misc/xmlhttp-censustract.html +++ b/httemplate/misc/xmlhttp-censustract.html @@ -6,7 +6,9 @@ my $DEBUG = 0; my $conf = new FS::Conf; my $return = {}; -my $url = "http://data.fcc.gov/api/block/find?format=json&censusYear=" . $cgi->param('census_year') . "&latitude=" . $cgi->param('lat') . "&longitude=" . $cgi->param('lon'); + +## 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; |