0

I have function when executed will plot routes on a map from an array of values. I am using Google maps *Directions Render*. From research i have seen, to render multiple routes on a map i need to create a Directions Render for each route in the array.

I am trying to create a Directions Render for each route in the array so i have decided to concat the name of the of the variabe with the index 'i'. this does not work.

I created the Directions Render object as an array however when accessing the array to pot the points i am getting an error - TypeError: can't convert undefined to object directionsService[i] = new google.maps.DirectionsService();

If i create separate Directions Render e.g. DirectionsRender0,DirectionsRender1, DirectionsRender2... this works fine however there will be an instance where i am not sure how many Directions Render there will be.

Under is an example of my code:

function plotRoutes(){  

    var directionsDisplay = new Array();

    for (var i=0; i< startLoc.length; i++){

    var rendererOptions = {
        map: map,
        preserveViewport:true
    }
    directionsService[i] = new google.maps.DirectionsService();

    var travelMode = google.maps.DirectionsTravelMode.DRIVING;  

    var request = {
        origin: startLoc[i],
        destination: endLoc[i],
        travelMode: travelMode
    };  
        directionsDisplay[i] = new google.maps.DirectionsRenderer(rendererOptions);

        directionsDisplay[i].setMap(map);

        directionsService.route(request, function(response, status) {
        //console.log(response);

        if (status == google.maps.DirectionsStatus.OK){
            console.log(response);
            directionsDisplay[i].setDirections(response);

            }

        });
    }   

1 Answer 1

1

The problem is that all your callbacks share the same variable "i", and in each of them the value of "i" will be one past the number of points by the time the callbacks are called.

Create your callback in a separate function, like this:

  function makeRouteCallback(disp) {
    return function(response, status) {
      console.log(response);
      disp.setDirections(response);
    };
  }

Then set up the callbacks:

  directionsService.route(request, makeRouteCallback(directionsDisplay[i]));

That assures that the actual callback function used by the API will be the correct member of your array. The reference to "i" is evaluated at the time the callback is established, not when it's actually called. At that point, the callback function uses the stashed copy ("disp"), which will be private to each callback.

Sign up to request clarification or add additional context in comments.

3 Comments

i am still getting the error TypeError: can't convert undefined to object [Break On This Error] directionsService[i] = new google.maps.DirectionsService();
@dev_darin you forgot to declare "directionsService" perhaps? There's no declaration in the code you posted.
Oh sorry my mistake i had directionsService[i] instead of just directionsService no need to pace an index on directionsService. Thanks for your help and great answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.