perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.06k stars 347 forks source link

How to call L.Routing.control() many times? #676

Open dev1ninja opened 1 year ago

dev1ninja commented 1 year ago

Hi. I am going to add multiple L.Routing.Control() with 50 waypoints into my Map. Here is my Code

Promise.all(arr).then(values => {
        for (let i = 0; i < Math.ceil(result.length / 50); i++) {
            let arr = result.slice(i * 50, i * 50 + 50)
            setTimeout(() => {
                L.Routing.control({
                    waypoints: [startLocation, ...arr],
                    show: false,
                }).on('routeselected', function (e) {
                    let route = e.route;
                    wb.SheetNames.push(`OSRM${i}`)
                    console.log("Route Result sub: ", route)
                    const ws_data = [
                        [
                            "Type",
                            "Distance",
                        ],
                        ...route.instructions.map(item => [
                            item.type,
                            item.distance,
                        ])
                    ]
                    let ws = XLSX.utils.aoa_to_sheet(ws_data);
                    wb.Sheets[`OSRM${i}`] = ws;
                }).addTo(map);
            }, i*1000)
        }
    })

As you can see on my code -> I'm trying to set many L.Routing.Control()(more than 500) with 50 waypoints in my map. So in this case, I need to send the requests to Nominatim Server to get the Routing result and Geocoding data. I should send more than 2000 requests in a time, but I can see, if I do like that, my IP has been blocked. How can I solve this problem? Also I would like to know how can I use Paid Version. Where can I subscribe and get API key? Thanks in advance

curtisy1 commented 1 year ago

Hey, 500 controls seems a bit excessive. What do you need them for exactly?

I should send more than 2000 requests in a time, but I can see, if I do like that, my IP has been blocked.

Nominatim, or the publicly available OSM servers aren't really made for heavy use. Their usage policy explicitly states that doing the same requests over and over will eventually get you blocked

How can I solve this problem?

If you want to continue using nominatim and OSRM, you need to set up your own instances. For testing purposes, I recommend a docker instance but you can also use a dedicated server and follow the installation instructions. Same goes for OSRM

The other option, as you mentioned is using a paid service like Mapbox or GraphHopper which might get you a few more leeway in the amount of requests