0

I am trying to take the results from a Google Maps DistanceMatrix api lookup into a javascript array, but the problem I am having is I am getting the error :

 Uncaught ReferenceError: arrResults is not defined 

The lines im having issues with are :

 html += parseInt(elements[j].distance.text) + "<br />";
arrResults.push(elements[j].distance.text);

This is the whole code :

    <script type="text/javascript">
    $( document ).ready(function() {
        $( "#submit" ).click(function() {
        $("#my_map").gmap3({
      getdistance:{
        options:{ 
          origins:["pe219px","ng323rj"], 
          destinations:["pe219px","ng323rj"],
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.IMPERIAL
        },
        callback: function(results, status){
          var html = "";
          if (results){
            for (var i = 0; i < results.rows.length; i++){
              var elements = results.rows[i].elements;
              for(var j=0; j<elements.length; j++){
                switch(elements[j].status){
                  case "OK":
                  var sd=$(this).text();  
                    html += parseInt(elements[j].distance.text) + "<br />";
        arrResults.push(elements[j].distance.text);

                    break;
                  case "NOT_FOUND":
                    html += "The origin and/or destination of this pairing could not be geocoded<br />";
                    break;
                  case "ZERO_RESULTS":
                    html += "No route could be found between the origin and destination.<br />";
                    break;
                }
              }
            } 
          } else {
            html = "error";
          }
          $("#my_map").html( html );
        }
      }
    });
         }); });
    </script>

1 Answer 1

2

You lack a declaration of arrResults :

callback: function(results, status){
    var html = "";
    var arrResults = [];
    ...
Sign up to request clarification or add additional context in comments.

Comments

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.