Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Help with this json parse

I'm calculating the distance between two lat/lon set of points using the Google Directions API, and I'm not sure how to parse this.

Here is the url to get directions (its from my city to my parents)

http://maps.googleapis.com/maps/api/directions/json?origin=30.5166667,-86.4822222&destination=30.7619444,-86.5705556&sensor=false

And here is the JSON response

{
  "status": "OK",
  "routes": [ {
    "summary": "FL-85 N",
    "legs": [ {
      "steps": [ {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": 30.5166700,
          "lng": -86.4824400
        },
        "end_location": {
          "lat": 30.5196600,
          "lng": -86.4823800
        },
        "polyline": {
          "points": "ehgyDfbznOuQK",
          "levels": "BB"
        },
        "duration": {
          "value": 63,
          "text": "1 min"
        },
        "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eReynolds Ave\u003c/b\u003e toward \u003cb\u003e32nd St\u003c/b\u003e",
        "distance": {
          "value": 333,
          "text": "0.2 mi"
        }
      }, {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": 30.5196600,
          "lng": -86.4823800
        },
        "end_location": {
          "lat": 30.5220100,
          "lng": -86.4924900
        },
        "polyline": {
          "points": "{zgyDzaznO{Fdb@SVeEfZ",
          "levels": "B??B"
        },
        "duration": {
          "value": 130,
          "text": "2 mins"
        },
        "html_instructions": "Turn \u003cb\u003eleft\u003c/b\u003e at \u003cb\u003eE John Sims Pkwy\u003c/b\u003e",
        "distance": {
          "value": 1005,
          "text": "0.6 mi"
        }
      }, {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": 30.5220100,
          "lng": -86.4924900
        },
        "end_location": {
          "lat": 30.7619200,
          "lng": -86.5707200
        },
        "polyline": {
          "points": "qihyD`a|nOw@`Bu@t@_Bt@qU?GJcSMeKa@aHs@}HuAKSiDm@wD_@kFGoF^iF`AgG`Ceq@~\\cHrEaD~CsDbFcDvGs@fBkA`Eg@~BsHjf@kNl_Au@tD}AvFgBdEeB|C{DfFqDdDeIrE}Aj@yFvAkzDbZ{VbC}MhCs_A|Ug[rHgwA|]mZlIy~@x_@{|EbrBeSfIuDz@gLdA_FD_EMsf@cCmgAuEuoAcGqSu@uJy@uG{@mw@qPcRuDyOoCeFYsv@O[JwDJiEx@aDdAc@Xei@pd@k@Ve@D{AM",
          "levels": "B??@?@????????@???A????@????@??@?A???@???@@???A??@???@?????@???@?????B"
        },
        "duration": {
          "value": 1424,
          "text": "24 mins"
        },
        "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e at \u003cb\u003eFL-85 N\u003c/b\u003e",
        "distance": {
          "value": 29739,
          "text": "18.5 mi"
        }
      }, {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": 30.7619200,
          "lng": -86.5707200
        },
        "end_location": {
          "lat": 30.7619400,
          "lng": -86.5705600
        },
        "polyline": {
          "points": "_ewzD~ikoOC_@",
          "levels": "BB"
        },
        "duration": {
          "value": 34,
          "text": "1 min"
        },
        "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e at \u003cb\u003eE James Lee Blvd\u003c/b\u003e",
        "distance": {
          "value": 16,
          "text": "52 ft"
        }
      } ],
      "duration": {
        "value": 1651,
        "text": "28 mins"
      },
      "distance": {
        "value": 31093,
        "text": "19.3 mi"
      },
      "start_location": {
        "lat": 30.5166700,
        "lng": -86.4824400
      },
      "end_location": {
        "lat": 30.7619400,
        "lng": -86.5705600
      },
      "start_address": "308-408 Reynolds Ave, Niceville, FL 32578, USA",
      "end_address": "265-299 E James Lee Blvd, Crestview, FL 32539, USA",
      "via_waypoint": [ ]
    } ],
    "copyrights": "Map data ©2010 Google",
    "overview_polyline": {
      "points": "ehgyDfbznOuQK{Fdb@SVeEfZw@`Bu@t@_Bt@qU?GJcSMaFMeMgA}HuAKSyGcAsHQoF^iF`AgG`CyCvAkl@fZoErCgCpBsCxCmCvDcDvGs@fBkA`Eg@~BsHjf@kNl_Au@tD}AvFgBdEeB|C{DfFqDdDeIrE}Aj@yFvAkzDbZ{VbC}MhCs_A|Ug[rHgwA|]mZlI{pHd}CuDz@kHp@{BR_FDsl@qCmgAuEuoAcGqSu@uJy@uG{@mw@qPcRuDyOoCeFYsv@O[JwDJiEx@aDdAc@Xei@pd@k@VkADu@MC_@",
      "levels": "B@??@??@???@?????@?????A????@????@??@?A???@???@@A???@???@?????@???@??@???B"
    },
    "warnings": [ ],
    "waypoint_order": [ ]
  } ]
}

At the bottom, you'll see the correct distance, which is 19.3 miles. However, how do I know when I actually iterate to that one? Anyone else have experience with this? Or a more simple method? I have a function that calculates the distance as if you were flying, but that doesnt help in a lot of cases.

— asked November 17th 2010 by Josh Lewis
  • distance
  • driving
  • google
  • maps
0 Comments

2 Answers

  • Accepted Answer

    var url = 'http://maps.googleapis.com/maps/api/directions/json?origin=30.5166667,-86.4822222&destination=30.7619444,-86.5705556&sensor=false'; 
    var req = Ti.Network.createHTTPClient(); 
    req.open('GET', url);
    
    req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    req.onreadystate = function(){};
    req.onload = function() 
    { 
        try { 
            var parsedData = JSON.parse(req.responseText);
            Ti.API.info("distance.text " + parsedData.routes[0].legs[0].distance.text);
            Ti.API.info("distance.value " + parsedData.routes[0].legs[0].distance.value);
    
        }  catch(err) { 
             Ti.API.info('>>>>>>> Error In getToken ' + err ); 
        } 
    };
    
    req.send();
    
    — answered November 17th 2010 by Aaron Saunders
    permalink
    1 Comment
    • how can i do this in python?

      — commented November 2nd 2012 by Nightfury V
  • That worked great, thanks!

    Before I mark it as best, does the json read from bottom to top (since 0) is the right index? If not, how did you know to use that one?

    — answered November 18th 2010 by Josh Lewis
    permalink
    2 Comments
    • nope , he uses 0 , to point the first routes & objects array. Because the distance value is a child of legs , and legs is a child of routes.

      In this example , to get the distance , you will need to point all the parents before getting the value. To point them , you use index :) On the end , because you have a value name , you can't have index , and you point it , by adding it's name.

      — commented November 18th 2010 by Filip Dimitrovski
    • I have no clue what that means, but it works.

      — commented November 19th 2010 by Josh Lewis
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.