title: Parse Values from Google distance matrix API
body: <p>MY JS which follows calls a PHP asynchronously.</p>
  
  <pre><code>function gettravelDistanceTime()
  {
          var latitude="10.014381";
          var longitude="76.334256";
          var toLatitude="10.014307";
          var toLongitude="76.312421";
          var curPosition=latitude+","+longitude ;
          var toPosition=toLatitude+","+toLongitude;
          //var key="AIzaSyBKLWZ3waR9GopA320z5IhQoqKKzeThns4";
  
  /*   Working sample 
  
  var distancedata=http://maps.googleapis.com/maps/api/distancematrix/json?units=metric&amp;origins=10.014381,76.334256&amp;destinations=10.014307,76.312421&amp;key=AIzaSyBKLWZ3waR9GopA320z5IhQoqKKzeThns4;
  
  */
  
  var distancedata="http://aiswarya.wehubs.website/alien/new/proxyapi.php?curPosition="+curPosition+"&amp;toPosition="+toPosition;
  
  console.log(distancedata);
  
  
  var data_val={'curPosition' : curPosition,
              'toPosition' : toPosition
  
         }; 
  /*
  
  var jqxhr123 = $.getJSON(distancedata, function(data123) {
  
  
      //Fetching distance and time from the API result
  
        //  var dis=data.rows[0].elements[0].distance.text;
         // var time=data.rows[0].elements[0].duration.text;
  
      //console.log(dis);
      //console.log(time);
  
      console.log(data123);
  
  
      })
              .error(function(err) {
  
                  console.log("Error");
              })
              .complete(function() {
                  console.log("Success");
              });
  
  
  
          */
  
  
  
                  $.ajax({
                      type: "POST",   
                      url: "proxyapi.php", 
                      //dataType: 'json',
  
                      data: data_val,
   success: function(result) {
  
  
            console.log(result);
            console.log(typeof(result));
          //  var dis=result.rows[].elements[].distance.text;
          //var time=result.rows[].elements[].duration.text;
          //console.log(dis);
          //console.log(time);
  
  
  
          }  ,                         
  
  error: function(err) {
       console.log('error'); 
        console.log('err'); 
      } 
  
  
  
  
  
  
  
      })
  
  
  
      }
  </code></pre>
  
  <p>My PHP is as follows</p>
  
  <pre><code>&lt;?php
  
  
  $GOOGLE_KEY="AIzaSyDbNk8Jbtqt8-cNOW5XI3RC9ub2MbCLJg4";
  
  $curPosition=$_POST['curPosition'];
  $toPosition=$_POST['toPosition'];
  
  
  
  //echo $toPosition;
  
  $distancedata="https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&amp;origins=".$curPosition."&amp;destinations=".$toPosition."&amp;key=".$GOOGLE_KEY;
  echo $distancedata;
  
  $data=file_get_contents($distancedata);
  
  
  $result=json_encode($data);
  echo $data;
  
  ?&gt;
  </code></pre>
  
  <p>This is the JSON response I get back to my JS from the google distance matrix api. </p>
  
  <p>{</p>
  
  <pre><code>     "destination_addresses" : [ "Mosque Road, Padivattom, Edappally, Ernakulam, Kerala 682024, India" ],
     "origin_addresses" : [
        "Manapurath Building, 11/195A, Civil Line Rd, Opposite Petrol Pump, Kunnumpuram, Padamughal, Kakkanad, Kochi, Kerala 682021, India"
     ],
     "rows" : [
        {
           "elements" : [
              {
                 "distance" : {
                    "text" : "3.1 km",
                    "value" : 3133
                 },
                 "duration" : {
                    "text" : "8 mins",
                    "value" : 506
                 },
                 "status" : "OK"
              }
           ]
        }
     ],
     "status" : "OK"
  }   
  </code></pre>
  
  <p>The data is being consoled in the JS, but I can't access the variables like the follows. I tried to access the distance and time parameters like this.</p>
  
  <pre><code>      var dis=data.rows[0].elements[0].distance.text;
        var time=data.rows[0].elements[0].duration.text;
  </code></pre>
  
  <p>But it doesn't work. Can anyone help me?</p>