I have an array with coordinates ([[54.73, 55.74], [55.66, 54.73]...]). I want to get route from each point to another. E.g. if I have 3 points a1, a2, a3, I want to calculate routes a1a2, a1a3, a2a1, a2a3 etc. I need this to make distance matrix later. Here is my loop where coords is array of coordinates

function routingLoop() {
            for (var i = 0; i < coords.length; i++){
                for (var j = 0; j < coords.length; j++){
                    var routing;
                    routing = L.routing.control({
                            L.latLng(coords[i][0], coords[i][1]),
                            L.latLng(coords[j][0], coords[j][1]),

But I get this error Routing error: Object { status: -3, message: "TypeError: this._altContainer is undefined" }. Any ideas what's wrong with that code?

  • Its possible that its a problem that is not in that code. For example, maybe you haven't set Leaflet up correctly? Maybe try doing a really simple example (without the loops) that demonstrates the problem in one of the javascript test sites and posting that. – BradHards May 25 at 7:57
  • I'm pretty sure everything is fine with leaflet because it works well when I try routing without loop – indeedme May 25 at 8:06
  • Does it work if you avoid trying to route from i to j where i and j are the same? For example, check if i === j and continue the loop. – BradHards May 25 at 8:23
  • @BradHards as it turned out LRM can't route like a1a1 (distance == 0, time == 0), but I need it so I just used mapbox direction api for that purpose – indeedme May 25 at 12:27

It might look dumb but that's how I did it after all. Hope someday someone will find it useful. Basically I just calculated routes using mapbox directions api (it's free and you can send up to 300 requests per/min) and used it's summary (btw it appears to me that the most comfortable way is to use LRM ONLY to draw nice looking routes and if you need some data about it, better use OSRM or another routing api)

function getRoutSummary(i, j) {
                var url = 'https://api.mapbox.com/directions/v5/mapbox/driving/' +
                        coords[i][1] + ',' + coords[i][0] + ';' +
                        coords[j][1] + ',' + coords[j][0] +
                        '?geometries=geojson&access_token=YOUR TOKEN HERE';

                var req = new XMLHttpRequest();    
                req.responseType = 'json';    
                req.open('GET', url, true);

                req.onload = function () {
                    var jsonResponse = req.response;
                    var distance = jsonResponse.routes[0].distance;
                    console.log('Distance between ' +
                        'coords[' + i  +'][1]' + ',' + 'coords[' + i + '][0]' + ' and ' +
                        'coords[' + j  +'][1]' + ',' + 'coords[' + j + '][0]' +
                        ' is ' + distance);                    

console.log only to make me comfortable. And then iterate through array of coordinates

function getAllRoutes(){
            console.log('Begin getAllRoutes');
            for (var i = 0; i < coords.length; i++){
                for (var j = 0; j < coords.length; j++){
                    getRoutSummary(i, j);
  • Questions shuld go to questions, not answers. Nobody will look for questions in answers. – TomazicM May 30 at 12:16
  • Forgot to delete it, sorry – indeedme May 30 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.