1

I want to create a map like this.

My code is done and works BUT the server limits me to 40 requests/minute, so I need to slow my requests down. I found out that I reach the server limit with even one City in my .csv list (it should just send one request).

The Errors show me that my loop doesn't stop sending requests with the same city to the server, until the server limit is reached.

I want the loop to execute each .length once and just once.

Whats wrong?

var Städte;
var data;
var profile = 'driving-car'
var preference =  'fastest'

function setup() {
    createCanvas(2000,2000);   
    loadJSON('URL', gotData);
    Städte = loadStrings ('v3.1.csv'); 
}

function gotData(data) {
    var route = data.features;  
    for (var j = 0; j <= 1; j++ ) {
        var citydata = Städte[j].split(/,/);   
        var lon = citydata[3];
        var lat = citydata[2];

        loadJSON('URL', gotData);

        beginShape(); 
        for (var i = 0; i < route[0].geometry.coordinates.length; i=i+500) {
            var x = route[0].geometry.coordinates[i][0];
            var y = route[0].geometry.coordinates[i][1];      
            noFill();
            vertex(x*100-200,-y*100+6000); 
        } 
        endShape();   
    }
}

my code

foliran
  • 75
  • 6

1 Answers1

1

If I'm reading your code correctly (which is a little hard because it's not properly formatted), then you're redoing the request inside the gotData() function:

var lat = citydata[2];

loadJSON('URL', gotData);

beginShape();

So your code works like this:

  1. The setup() function is called.
  2. You call the loadJSON() function, which makes the request and then calls the gotData() function.
  3. The gotData() function is called.
  4. Inside the gotData() function, you call loadJSON() again, which makes another request.
  5. The gotData() function is called again, and again, and again...

This is why you're exhausting your quota. Why do you have that second call to loadJSON() in there? Can you just remove it?

Also note that you should get into the habit of debugging your code and checking your developer tools to understand problems like this.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Sorry for the formatting - im new to this. Yes you probably found my mistake! I load the JSON in steup() to make it accessible. Then i need to load again to insert the lon and lat from the .csv (I get the lon and lat in the loop in gotData) so i cant remove the loadJSON in setup() and i cant remove it in my gotData? – foliran Oct 21 '18 at 15:58
  • @foliran What is the second call to `loadJSON()` doing though? How is it different from the first call? It doesn't sound like you actually need that second call at all. – Kevin Workman Oct 21 '18 at 16:01
  • Of course the first: loadJSON('http://localhost:4567/2000/https://api.openrouteservice.org/directions?api_key='+ Key '&coordinates='+''8.2472526,49.9928617|11.789879,50.1905748&profile=' + profile + '&preference=' + preference + '&format=geojson', gotData); its quite long.. -i use localhost to delay my request (-> max 40 request / minute) -next is URL with my APIKey and Parameters for the route (driving, shortest etc.) -then gotData – foliran Oct 21 '18 at 16:54
  • the second: loadJSON ('http://localhost:4567/2000/https://api.openrouteservice.org/directions?api_key='+ Key +'&coordinates=' + lon + ',' + lat + '|11.789879,50.1905748&profile=' + profile + '&preference=' + preference + '&format=geojson', gotData); Its the same Link but this time i insert the lon and lat from my .csv so maybe i dont need both but i cant delete them without loosing my resut – foliran Oct 21 '18 at 16:57
  • @foliran You need to prevent the code from calling `loadJSON()` after the second time. Maybe use a second function instead of using `gotData()` for both of them? Or maybe add an `if` statement? – Kevin Workman Oct 21 '18 at 17:00
  • Wow it might be the quick and dirty way but it works and i guess that it will do the job. i just added a second funtion getData(data) and its fine Thanks! – foliran Oct 21 '18 at 17:05
  • @foliran I don't think it's a hack at all. Maybe give the functions more descriptive names, but other than that I think it's fine. – Kevin Workman Oct 21 '18 at 17:08