Jafo
Jafo

Reputation: 1331

gmaps.js returning old value for getRoutes

If there are multiple legs, the distance reported for each leg is the same. Any idea why? It reports the distance between the first two legs right, but then the distance between every other leg is the same as the first leg. Probably just over-caffeinated at this point, but I can't see why.

Here is the code:

function marker(map, lat, lng, title, name) {
map.addMarker({
    lat: lat,
    lng: lng,
    title: title,
    click: function (e) {

        legs.push({destination: name, latitude: lat, longitude: lng, distance: null, time: null});

        var next_stop;

        if (legs.length > 1)
        {
            $.each(legs, function (index, value)
            {
                // Mileage In Between
               if (legs[(index + 1)])
               {
                   next_stop = legs[(index + 1)];

                map.getRoutes(
                    {
                        origin: [value.latitude, value.longitude],
                        destination: [next_stop.latitude, next_stop.longitude],
                        callback: function (e)
                        {
                            var time = 0;
                            var distance = 0;
                            for (var i=0; i<e[0].legs.length; i++)
                            {
                                time += e[0].legs[i].duration.value;
                                distance += e[0].legs[i].distance.value;
                            }

                            var miles = distance/1609.34;

                            var kilometers = distance/1000;

                            var timer = time/60;

                            legs[(index + 1)].distance = miles.toFixed(2);

                            alert(timer.toFixed(2) + " and " + miles.toFixed(2));
                        }
                    }
                );
            }

            }
            );
        }

        console.log(legs);

        a.$set('stops', legs);

    }
});
}

Upvotes: 0

Views: 157

Answers (1)

Jafo
Jafo

Reputation: 1331

Sorry for wasting your time everyone. Apparently this is a known, unfixed bug with gmap.js:

https://github.com/hpneo/gmaps/issues/373

The fix is to find this code in gmaps.js:

    if (options.callback) {
    options.callback(self.routes, result, status);
  }

And replace it with:

    if (options.callback) {
    options.callback(result.routes, result, status);
  }

Upvotes: 1

Related Questions