xxxxxxxxxx
var NUMRECORDS = 100; // this is how many records
var NYCAPIURL = 'https://data.cityofnewyork.us/resource/43nn-pn8j.json'; // restaurant inspection database
var GEOCODEURL = 'https://www.mapquestapi.com/geocoding/v1/batch?key=rDz5dqQpjbkQxzs1ktmvT3qcaw0QTxNf&inFormat=kvp&outFormat=json&thumbMaps=false';
var thestuff = {};
var locationdata = {};
var isready = false;
var fontsize = 9;
function preload() {
NYCAPIURL+='?$limit='+NUMRECORDS;
thestuff = loadJSON(NYCAPIURL, goforit);
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(255);
stroke(0);
fill(0);
textSize(fontsize);
}
function draw() {
if(isready)
{
background(0);
fill(255);
let x;
let y;
let s;
for(let i =0;i<NUMRECORDS;i++)
{
fill(255);
x = map(thestuff[i].lng, thestuff.lngmin, thestuff.lngmax, 0, width);
y = map(thestuff[i].lat, thestuff.latmin, thestuff.latmax, height, 0);
ellipse(x, y, 20, 20);
s=thestuff[i].dba;
text(s, x+5, y-fontsize);
s=thestuff[i].cuisine_description;
text(s, x+5, y);
}
}
}
function goforit() { // response function
dogeocode();
}
function dogeocode() {
for(let i =0;i<NUMRECORDS;i++)
{
let a = thestuff[i].building + ' ' + thestuff[i].street + ' ' + thestuff[i].boro + ' ' + thestuff[i].zipcode;
GEOCODEURL+='&location='+a;
}
locationdata = loadJSON(GEOCODEURL, geocoded);
}
function geocoded()
{
thestuff.latmin = 1000; // start too high
thestuff.latmax = -1000; // start too low
thestuff.lngmin = 1000;
thestuff.lngmax = -1000;
for(let i = 0;i<NUMRECORDS;i++)
{
thestuff[i].lat = locationdata.results[i].locations[0].latLng.lat;
thestuff[i].lng = locationdata.results[i].locations[0].latLng.lng;
}
thestuff.latmin = 40.7527 - 0.2;
thestuff.latmax = 40.7527 + 0.2;
thestuff.lngmin = -73.9772 - 0.2;
thestuff.lngmax = -73.9772 + 0.2;
isready = true;
}