Iain Simpson
Iain Simpson

Reputation: 8121

Issue putting results into array in Javascript / Google Maps Api

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>

Upvotes: 0

Views: 70

Answers (1)

davidkonrad
davidkonrad

Reputation: 85518

You lack a declaration of arrResults :

callback: function(results, status){
    var html = "";
    var arrResults = [];
    ...

Upvotes: 2

Related Questions