Android Question Can someone make a library for Google api directions ?

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,

i think that B4A is very good prepared to use the google directions api directy. With the use of jsontree its easy like never before to get all the values, which come back from google.
 
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
I'm not an expert. I don't know json. so, how can i get relevant info ? can you give me an example ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Just do a call to googles api with httputils.

Example url: http://maps.googleapis.com/maps/api/directions/json?origin=Am Steinberg 99, 52353 Düren, Germany&destination=Hochhausring 21, 52076 Aachen, Germanyl&sensor=false



See Googles Api documentation about the available parameters.

Replace the address in ORIGIN and DESTINATION to your needs...

You get an result of:
{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 50.8402068,
"lng" : 6.4591769
},
"southwest" : {
"lat" : 50.7103698,
"lng" : 6.1483801
}
},
"copyrights" : "Kartendaten © 2014 GeoBasis-DE/BKG (©2009), Google",
"legs" : [
{
"distance" : {
"text" : "38,4 km",
"value" : 38436
},
"duration" : {
"text" : "33 Minuten",
"value" : 1980
},
"end_address" : "Hochhausring 21, 52076 Aachen, Deutschland",
"end_location" : {
"lat" : 50.7104793,
"lng" : 6.1796888
},
"start_address" : "Am Steinberg 99, 52353 Düren, Deutschland",
"start_location" : {
"lat" : 50.8336047,
"lng" : 6.4404509
},
"steps" : [
{
"distance" : {
"text" : "35 m",
"value" : 35
},
"duration" : {
"text" : "1 Minute",
"value" : 5
},
"end_location" : {
"lat" : 50.83329690000001,
"lng" : 6.4405766
},
"html_instructions" : "Auf \u003cb\u003eAm Steinberg\u003c/b\u003e nach \u003cb\u003eSüden\u003c/b\u003e starten",
"polyline" : {
"points" : "_mguHy{hf@z@Y"
},
"start_location" : {
"lat" : 50.8336047,
"lng" : 6.4404509
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "43 m",
"value" : 43
},
"duration" : {
"text" : "1 Minute",
"value" : 15
},
"end_location" : {
"lat" : 50.83341799999999,
"lng" : 6.4411577
},
"html_instructions" : "Nach \u003cb\u003elinks\u003c/b\u003e abbiegen, um auf \u003cb\u003eAm Steinberg\u003c/b\u003e zu bleiben",
"maneuver" : "turn-left",
"polyline" : {
"points" : "ckguHs|hf@WsB"
},
"start_location" : {
"lat" : 50.83329690000001,
"lng" : 6.4405766
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,2 km",
"value" : 234
},
"duration" : {
"text" : "1 Minute",
"value" : 56
},
"end_location" : {
"lat" : 50.83136529999999,
"lng" : 6.4416505
},
"html_instructions" : "\u003cb\u003eRechts\u003c/b\u003e abbiegen auf \u003cb\u003eBirkesdorfer Straße\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "{kguHg`if@RKNGLGFAFCNCNANAP?LApCALANALABAJCHEBADADEDEB?BABALM"
},
"start_location" : {
"lat" : 50.83341799999999,
"lng" : 6.4411577
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,6 km",
"value" : 566
},
"duration" : {
"text" : "1 Minute",
"value" : 53
},
"end_location" : {
"lat" : 50.8288851,
"lng" : 6.4482186
},
"html_instructions" : "Im Kreisverkehr \u003cb\u003edritte\u003c/b\u003e Ausfahrt nehmen, um auf \u003cb\u003eBirkesdorfer Straße\u003c/b\u003e zu bleiben",
"maneuver" : "roundabout-right",
"polyline" : {
"points" : "a_guHicif@B@@?@?@?@?@ABA@CBC@CBE?G?C?GAEAEACAAAA?c@CUAI?G?A?EAQ@U?K@G@MBMDODSDKDMfCyFjCeGv@iBN_@JUL]DMTw@BOH_@Jk@D_@B[Di@Du@"
},
"start_location" : {
"lat" : 50.83136529999999,
"lng" : 6.4416505
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,6 km",
"value" : 642
},
"duration" : {
"text" : "1 Minute",
"value" : 64
},
"end_location" : {
"lat" : 50.82785459999999,
"lng" : 6.4570149
},
"html_instructions" : "Weiter auf \u003cb\u003eHovener Straße\u003c/b\u003e",
"polyline" : {
"points" : "qofuHkljf@@K?G@}@@e@?WAc@Am@Aa@AS?AAe@Ca@A]?UA??ECeACk@A_@?{@?[@Y@]@YFs@n@sELsABS@]Ds@DyAD{@Js@@MDQFa@H]@CNo@H_@\\}ANs@Ps@"
},
"start_location" : {
"lat" : 50.8288851,
"lng" : 6.4482186
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "31 m",
"value" : 31
},
"duration" : {
"text" : "1 Minute",
"value" : 9
},
"end_location" : {
"lat" : 50.8278293,
"lng" : 6.4574422
},
"html_instructions" : "Leicht \u003cb\u003elinks\u003c/b\u003e abbiegen, um auf \u003cb\u003eHovener Straße\u003c/b\u003e zu bleiben",
"maneuver" : "turn-slight-left",
"polyline" : {
"points" : "aifuHiclf@Dg@?Q?MAM"
},
"start_location" : {
"lat" : 50.82785459999999,
"lng" : 6.4570149
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,7 km",
"value" : 656
},
"duration" : {
"text" : "1 Minute",
"value" : 79
},
"end_location" : {
"lat" : 50.8331733,
"lng" : 6.4591769
},
"html_instructions" : "\u003cb\u003eLinks\u003c/b\u003e abbiegen auf \u003cb\u003eNordstraße\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "}hfuH_flf@sBjAgCvA_AMsDo@_@GA?gBWyAW[KQIOKOKMK[c@eCkDIY}@mAQW"
},
"start_location" : {
"lat" : 50.8278293,
"lng" : 6.4574422
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,9 km",
"value" : 886
},
"duration" : {
"text" : "1 Minute",
"value" : 74
},
"end_location" : {
"lat" : 50.83980529999999,
"lng" : 6.4528564
},
"html_instructions" : "\u003cb\u003eLinks\u003c/b\u003e abbiegen auf \u003cb\u003eB56\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "ijguH{plf@IR{@nB_BnDOZYh@m@bAKRi@z@[f@[f@[d@KN[^[^Y^i@n@a@d@c@d@EBA@k@h@SPYRc@\\e@ZsBlAA?m@ZOFWJc@N_@Jo@PSDMBIBE@E?KBC?E@E@MBMB}Bb@"
},
"start_location" : {
"lat" : 50.8331733,
"lng" : 6.4591769
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "21,1 km",
"value" : 21123
},
"duration" : {
"text" : "12 Minuten",
"value" : 697
},
"end_location" : {
"lat" : 50.8047127,
"lng" : 6.1695668
},
"html_instructions" : "Leicht \u003cb\u003erechts\u003c/b\u003e abbiegen auf \u003cb\u003eA4\u003c/b\u003e Richtung \u003cb\u003eAachen\u003c/b\u003e",
"polyline" : {
"points" : "yshuHkikf@MGMIGEGGEGEICi@IqAEuAAe@Aa@?E?C?G@[@OBM@KDIBIDGBEDEDCDEJEHAH?H?JDFBHFFHDHFPDJFZLbADTHRHPHPf@zDFb@n@dFl@lE`BxL@JDZ@HjAbJ@HBX@DDZ?BDXrArK@JFd@@BFj@Fb@D\\F^J~@F`@Fh@p@~F`@hDLtANtAFh@Dj@PnBDl@@J@F?FBTLhBB`@Dh@@L@VBh@X|GFnABnADnABnA@nA@hA@hA?lB?nB?rCC~E?bI?H?V?P?J?hC@jC?tC?LC~^?`FArOI`zA?BFl]@zo@@hL@hM?hD?|Q?pBAfH?p@?j@?H?Z?`@@~H?nI@vEB|D@`F?l@?H?z@?N@pC?Z?B@p@?vA@lC@h@@zFDdJD|G?f@@`A?JBlA?TB|@B~@D|@@b@FdADpAXdEZdFPzCRzD?@d@zKb@jJBl@n@tMZhGLzDXbH@h@?@Bj@b@fKVjFBj@FrAJ|BB|@DnAB|@@X?J@R@^@d@@`@@zB?v@?Z?nA?pAApAC|CAjBAlACxB@JAN?FAd@?f@?@?JANAx@C`CEnEA`AQxLKnIAlE?rE@pB?@@`A?B@x@?B?TBv@BlABx@Bv@DjAFtAHtALpBLnBJrAJrAJnAJlARhBLfAP`BLfAn@`Fj@vEBT\\`CJ`AFd@LnA@FHt@Hr@Hp@HjAR`CTrCF~@@XFlAFlAFnADhABhADbBJ~EZ`O?BTnLDjB@`@HjFB`A@ZDpAJbDBlAFjABr@Dr@Dx@HnAD|@Fz@b@~FP`Bp@fI?@@J?@?D@T@D?D@Xb@nGF|@Dl@XxHPnDH~A@RDlAF`BVrHb@nMFjBDv@?@Br@hD|kABv@?TDlAdDjcAb@bL@TBb@?BDr@JpBZ|Fz@~NB~@|@tTHnDBj@?H?HBj@F|Ax@lY@r@@RXrIBn@@P?\\@b@@DXfJ@f@F~ADx@@^F~@D~@R|CNzBH`AFp@D\\Fd@Hz@J`AL|@Jv@LbAPhANfAPjARfARdAP~@R`AP~@RbAl@dCl@vBLb@^xAZbANd@X|@Rn@@DXx@Z|@DJ^`AVr@Tf@DJ~FxN~HtR@DfKbXtAjDFNz@vB^z@dAhCTn@Rb@`@`Aj@tA?BLXTl@Xr@b@dAp@bBh@rA\\z@fAjCvArDv@lBbAfCx@vBz@zBxA|DnBjF|@~Bf@rATl@HRXx@Vp@JVL\\L`@L\\J^Pf@Tt@@BZfAt@fCl@~BNn@H\\Pl@ThA\\~AZxAVnAN~@Nv@N|@Hd@L|@V|AFb@`@pCPlAHr@D^D`@Dd@JjALnAJhA?DBXd@|F~@dKL|AN|ANzANzARfCNfBfA~MH|@XlD?HBP@FLfBD^BdCP|C`@nI?BTjFLhH?LBvC?nAAbA?B?X?B?d@?D?B?J?FAB?LCrC"
},
"start_location" : {
"lat" : 50.83980529999999,
"lng" : 6.4528564
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "6,8 km",
"value" : 6788
},
"duration" : {
"text" : "4 Minuten",
"value" : 253
},
"end_location" : {
"lat" : 50.7545442,
"lng" : 6.1504127
},
"html_instructions" : "Am Autobahnkreuz \u003cb\u003e4-Kreuz Aachen\u003c/b\u003e \u003cb\u003erechts\u003c/b\u003e halten und den Schildern \u003cb\u003eA44\u003c/b\u003e in Richtung \u003cb\u003eBrüssel/Aachen-Süd/Liège/Lüttich\u003c/b\u003e folgen",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "mxauHy~sd@YtAITUV]PUBSEUOMQS]Ss@Ie@Ai@Dc@D[JYPYXUj@WXDn@JXBB@B?B?B@B?@@N@D@@?XDF?J@p@Jf@FjALrQxArKjAl@HR@fAHL@j@Dj@Bj@@lCFf@?F?l@?l@?n@Cl@C^C^C\\C^E^C|@KLCp@K\\GbAQB?HAXGB?@?JCREREv@Ov@Sv@Sv@Wv@Wv@Yv@[FCl@Wv@_@t@_@j@Y`Ak@lBiABA`@W`@Yj@a@z@o@NKh@a@VUNOb@a@BADEbA}@nAgA\\]BAJIRS@A^[LKBCTQJKDCRObBkAj@_@h@]fAo@\\QPMp@_@bAg@f@WXM`@Q^Op@Up@Ux@Wv@KXI`@K^IXI^I^GTGDAvAWr@I|@Mh@El@GXCv@E|@E`AE`AAT?@?N?X?V?~@@l@@T@\\?n@D^@h@DZ@bEb@p@LVD@@xCn@~@V|C`AdBr@dBz@lBbA|BtAhAt@`Av@RN`At@jAbAt@l@v@r@b@d@VVj@n@l@p@X\\d@j@Z`@f@r@l@x@T\\LP`@l@`@l@^l@`@n@^n@`@t@^t@Zj@R`@^t@l@pABDd@fAPb@Zt@h@rABF^bA^bAJZb@tAhAjDL`@x@dCFP`AtC~@nC`BvE@BL\\@BN\\DJnAvCb@dAt@fBTf@h@nAj@lAj@lAbArBh@fA~@jB~@hBj@dAn@jAfApBr@nAf@z@h@x@R\\f@v@f@v@fBjC`CjDh@n@h@l@hBbC"
},
"start_location" : {
"lat" : 50.8047127,
"lng" : 6.1695668
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,4 km",
"value" : 382
},
"duration" : {
"text" : "1 Minute",
"value" : 44
},
"end_location" : {
"lat" : 50.7558873,
"lng" : 6.149960999999999
},
"html_instructions" : "Bei Ausfahrt \u003cb\u003e3-Aachen-Brand\u003c/b\u003e Richtung \u003cb\u003eStolberg/B258\u003c/b\u003e fahren",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "{~wtHagpd@x@fCFNBHBFBH@HBHBJ@H@L@J?N?JALANALCHAFGPEHEFEFCBEDE@IDGBK@G?G?GAEAGEGCEEGE[WAA{@gAW[gAiAWq@AEAEAEAEAO"
},
"start_location" : {
"lat" : 50.7545442,
"lng" : 6.1504127
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2,7 km",
"value" : 2692
},
"duration" : {
"text" : "4 Minuten",
"value" : 247
},
"end_location" : {
"lat" : 50.7392855,
"lng" : 6.1750169
},
"html_instructions" : "\u003cb\u003eRechts\u003c/b\u003e abbiegen auf \u003cb\u003eTrierer Straße\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "igxtHgdpd@HUt@mB@ELa@Pe@Tm@Vo@BKDKJUFQDKHE?Al@iBHWLe@AMb@mABGRm@^iAVy@h@{A`AwCf@_BxAkD?AjAmDrA_ETu@z@cDRg@Zu@Tu@d@mAtDuKh@}AnB_GXy@FOhByFbAwC@AHU|CeJL]h@{AVs@^iARm@Z_A~@mC\\aAXq@Re@Tc@h@q@RUHGNONMNKVORK\\M|@SdAUNCfEaA@ATERGTClA[RGDA@?NGHCHC`Bc@hAYhAYvCu@FAFAhBe@TE\\I"
},
"start_location" : {
"lat" : 50.7558873,
"lng" : 6.149960999999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2,1 km",
"value" : 2088
},
"duration" : {
"text" : "3 Minuten",
"value" : 186
},
"end_location" : {
"lat" : 50.7273208,
"lng" : 6.188148699999999
},
"html_instructions" : "Weiter auf \u003cb\u003eNapoleonsberg\u003c/b\u003e",
"polyline" : {
"points" : "q_utH{`ud@j@MhAYz@QdAY^EVCVAR?JBD?ZHPDRHPLPLTR^VfB|APLVT\\TNHRJTJH?FBLD\\Jb@Hp@D^@nCFNALANGRIHGJKNULYHUFUHm@D[Di@Dq@Fy@Di@JqALiAD_@Ho@Fe@Jc@H[FWRu@HYHSHWP_@Ra@HQT]JQPYJMNSNQRQRKLEPENAN?`@@n@Dh@Fl@D`@B^BRAHALAPG@?VOPMHG`@[l@e@b@a@FEBEHKDEPWTk@P_@Vs@Re@JWH]F]Fa@BOB[BY@i@@_@?SAYAWCa@Ec@G]CIUoAYeBG_@CQAA?CCWAYAO?O@c@Fk@Jg@Nm@Z{An@cDVa@Li@Pq@Ja@FSJS"
},
"start_location" : {
"lat" : 50.7392855,
"lng" : 6.1750169
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1,7 km",
"value" : 1669
},
"duration" : {
"text" : "2 Minuten",
"value" : 112
},
"end_location" : {
"lat" : 50.7129428,
"lng" : 6.1856462
},
"html_instructions" : "Weiter auf \u003cb\u003eIternberg\u003c/b\u003e",
"polyline" : {
"points" : "wtrtH}rwd@BEDK@ADEHEDCPIFCJCPCVCDAXBL@L@FBHBDBLJNLDFLL?@LNLPV`@bA`BBBJNHJBBJJHHJHNHVJ`@JRB\\HfEp@x@ND?B@p@HB@ZBT@V@N?PALATC`@GrAQfBYp@IFAfJoAxEk@xAQd@GVCNCB?h@Cf@@V@N@ZFPDx@XlBx@jBx@dDrAtEnB"
},
"start_location" : {
"lat" : 50.7273208,
"lng" : 6.188148699999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,2 km",
"value" : 166
},
"duration" : {
"text" : "1 Minute",
"value" : 22
},
"end_location" : {
"lat" : 50.7126777,
"lng" : 6.1833294
},
"html_instructions" : "\u003cb\u003eRechts\u003c/b\u003e abbiegen auf \u003cb\u003eAlbert-Einstein-Straße\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "{zotHicwd@FhBFdA@RDn@Dx@HbAJ|A"
},
"start_location" : {
"lat" : 50.7129428,
"lng" : 6.1856462
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,3 km",
"value" : 299
},
"duration" : {
"text" : "1 Minute",
"value" : 39
},
"end_location" : {
"lat" : 50.7103698,
"lng" : 6.1814408
},
"html_instructions" : "1. Abzweigung \u003cb\u003elinks\u003c/b\u003e nehmen, um auf \u003cb\u003eFlorastraße\u003c/b\u003e zu wechseln",
"polyline" : {
"points" : "gyotHytvd@bALH@h@LH@ZHNDPFPHHDJDHFHFHDHHHFHHFFHJBBNRRXV^RV^t@N\\Rf@"
},
"start_location" : {
"lat" : 50.7126777,
"lng" : 6.1833294
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,1 km",
"value" : 136
},
"duration" : {
"text" : "1 Minute",
"value" : 25
},
"end_location" : {
"lat" : 50.7104793,
"lng" : 6.1796888
},
"html_instructions" : "\u003cb\u003eRechts\u003c/b\u003e abbiegen auf \u003cb\u003eHochhausring\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDas Ziel befindet sich auf der linken Seite.\u003c/div\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "yjotH_ivd@CFMTEJCHCHCHCJCHAJAJAJAHAL?L?L?N?L@N@L@L@NBLBLDRDTBF"
},
"start_location" : {
"lat" : 50.7103698,
"lng" : 6.1814408
},
"travel_mode" : "DRIVING"
}
],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "_mguHy{hf@z@YWsBb@Sl@Q~@E|DGr@[XOF?JKDUEWCCCy@AY@{@P_A~GyO`B}Dh@uBZqCHgCGmFQmFBoCHmA|@gHP_EXoClByIBuA{FbDsF}@cFw@mAm@i@o@eCkDIYoAeBeAbCoBjEgAlBmB~C_BtBiCzCgA`AwEzCwAn@gCr@_AP[F}Bb@MGUOMOIs@OgDCqAFaAPg@TUh@Gd@ZZbARxARd@p@lEv@hGvCnTrApKlBhOjBxOfA~J^tE`@`Gh@fNLxHA`_@Axt@KtjBFp]DxpAA|c@Hzj@Z|q@N~EbAbQ~Dnz@h@jO~@~Sf@nNHzKMpSInHe@z_@?tONxIZnHr@hKx@nIzBbRxA`M|@rKXtFVfI|@tc@l@zWVlFz@hMhAzMl@lJz@vRxAlb@pD~oApErsATlEvA|V`AtVLnFfAj_@~@xYP`Fp@xKnAjL`A`HlAnGd@bCzA|FxAfFbDlJzQbd@fNd^xGlPbH`QpQfe@`DxI`AzC`CrI`AdE`BhIjCzP`AfKzDxc@nC~\\HdDr@lNTnFLvH@nH?hAAZC`Dc@jBs@h@i@Ac@a@g@qAKoAJ_A\\s@dAm@fBVN@xBXrBTrQxArKjA`AJtAJpGRjC?|COzCYlEs@t@O~EoAnDqAfEqBtEoCxCwBrGwFtBkBz@q@`GyDdDgBbCgAbBk@pBc@tBi@zA[rFu@|DWzCGbFDtCNnGx@xEfAbGtBrE~BfEjCbF`EhD~CxCjDfClDpBzCbCjE|BtEvAfDlBbFfC~HvHzTjHtPxEtJbFlJxCbFxHfLrA|AhBbCx@fCJXFPJh@Bt@It@Uj@URe@J]Iq@g@}CoD]cACU~@cCv@{Bl@}AVe@v@aCJs@zAmEjDmKxAmD~CmJpAyEn@}AzGwRhNya@|FuPl@wA~@uA|@{@xAu@zIoB`AShBe@xL_D|KkCbBK~@RlAx@pDxCvAv@|@TtANnDH`AUr@cA`@uBX_Eh@kFd@}Bp@{BdAqBx@mAb@c@`@Q`@Gp@@hDVjAA|@e@|BkBZ]f@cAh@sA^}@P{@RgB@wBS{BeAoGA}A~@}En@cDVa@^{Ab@oAVYj@UhAEl@Jh@d@h@p@nC|DZRx@VrHnA~@LxAFvAOtFw@`UuCtAKjBLjA^xErBzJbENnDV`EJ|AbALr@NfAXxA|@nBfCn@rARf@CFS`@GRMj@Gn@FtBVlA"
},
"summary" : "A4",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}


And with this code you can "read that json"

B4X:
im parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim status As String = root.Get("status")
Dim routes As List = root.Get("routes")
For Each colroutes As Map In routes
Dim summary As String = colroutes.Get("summary")
Dim bounds As Map = colroutes.Get("bounds")
Dim southwest As Map = bounds.Get("southwest")
Dim lng As Double = southwest.Get("lng")
Dim lat As Double = southwest.Get("lat")
Dim northeast As Map = bounds.Get("northeast")
Dim lng As Double = northeast.Get("lng")
Dim lat As Double = northeast.Get("lat")
Dim copyrights As String = colroutes.Get("copyrights")
Dim waypoint_order As List = colroutes.Get("waypoint_order")
Dim legs As List = colroutes.Get("legs")
For Each collegs As Map In legs
  Dim duration As Map = collegs.Get("duration")
  Dim text As String = duration.Get("text")
  Dim value As Int = duration.Get("value")
  Dim distance As Map = collegs.Get("distance")
  Dim text As String = distance.Get("text")
  Dim value As Int = distance.Get("value")
  Dim end_location As Map = collegs.Get("end_location")
  Dim lng As Double = end_location.Get("lng")
  Dim lat As Double = end_location.Get("lat")
  Dim start_address As String = collegs.Get("start_address")
  Dim end_address As String = collegs.Get("end_address")
  Dim start_location As Map = collegs.Get("start_location")
  Dim lng As Double = start_location.Get("lng")
  Dim lat As Double = start_location.Get("lat")
  Dim via_waypoint As List = collegs.Get("via_waypoint")
  Dim steps As List = collegs.Get("steps")
  For Each colsteps As Map In steps
  Dim html_instructions As String = colsteps.Get("html_instructions")
  Dim duration As Map = colsteps.Get("duration")
  Dim text As String = duration.Get("text")
  Dim value As Int = duration.Get("value")
  Dim distance As Map = colsteps.Get("distance")
  Dim text As String = distance.Get("text")
  Dim value As Int = distance.Get("value")
  Dim end_location As Map = colsteps.Get("end_location")
  Dim lng As Double = end_location.Get("lng")
  Dim lat As Double = end_location.Get("lat")
  Dim polyline As Map = colsteps.Get("polyline")
  Dim points As String = polyline.Get("points")
  Dim start_location As Map = colsteps.Get("start_location")
  Dim lng As Double = start_location.Get("lng")
  Dim lat As Double = start_location.Get("lat")
  Dim travel_mode As String = colsteps.Get("travel_mode")
  Next
Next
Dim warnings As List = colroutes.Get("warnings")
Dim overview_polyline As Map = colroutes.Get("overview_polyline")
Dim points As String = overview_polyline.Get("points")
Next
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You also can use the url

http://maps.googleapis.com/maps/api/distancematrix/json?origins=Am+Steinberg+99,+52353+Düren, Germany&destinations=Hochhausring+21,+52076+Aachen&mode=bicycling&language=fr-FR&sensor=false

To get an result of:

{
"destination_addresses" : [ "Hochhausring 21, 52076 Aix-la-Chapelle, Allemagne" ],
"origin_addresses" : [ "Am Steinberg 99, 52353 Düren, Allemagne" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "29,7 km",
"value" : 29663
},
"duration" : {
"text" : "1 heure 50 min",
"value" : 6626
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}

See JSON-Tree-Tutorial to get the source to read that json
->

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim status As String = root.Get("status")
Dim destination_addresses As List = root.Get("destination_addresses")
For Each coldestination_addresses As String In destination_addresses
Next
Dim origin_addresses As List = root.Get("origin_addresses")
For Each colorigin_addresses As String In origin_addresses
Next
Dim rows As List = root.Get("rows")
For Each colrows As Map In rows
Dim elements As List = colrows.Get("elements")
For Each colelements As Map In elements
  Dim duration As Map = colelements.Get("duration")
  Dim text As String = duration.Get("text")
  Dim value As Int = duration.Get("value")
  Dim distance As Map = colelements.Get("distance")
  Dim text As String = distance.Get("text")
  Dim value As Int = distance.Get("value")
  Dim status As String = colelements.Get("status")
Next
Next
 
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
Thank you very much ! I will try it. I don't want to be naughty but could you give me an example of call with httputils ?
 
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
Thank you DonManfred. I was successful with your help, using the following code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: Tratare json
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private descjson As Button
    Private citirejson As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("fatajson")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub descjson_Click
    Dim job As HttpJob
    job.Initialize("jobjson", Me)
    job.Download("http://maps.googleapis.com/maps/api/directions/json?origin=Calugareni%2012,%20Oradea,%20Romania&destination=Moldovei,%20Oradea,%20Romania&mode=walking&Language=ro-RO")
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "jobjson"
                citirejson.LoadHtml(Job.GetString)
                rezolvaJson(Job.GetString)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Sub rezolvaJson(jsonfile As String)
    Dim parser As JSONParser
parser.Initialize(jsonfile)
Dim root As Map = parser.NextObject
Dim status As String = root.Get("status")
Dim routes As List = root.Get("routes")
For Each colroutes As Map In routes
Dim summary As String = colroutes.Get("summary")
Dim bounds As Map = colroutes.Get("bounds")
Dim southwest As Map = bounds.Get("southwest")
Dim lng As Double = southwest.Get("lng")
Dim lat As Double = southwest.Get("lat")
Dim northeast As Map = bounds.Get("northeast")
Dim lng As Double = northeast.Get("lng")
Dim lat As Double = northeast.Get("lat")
Dim copyrights As String = colroutes.Get("copyrights")
Dim waypoint_order As List = colroutes.Get("waypoint_order")
Dim legs As List = colroutes.Get("legs")
For Each collegs As Map In legs
  Dim duration As Map = collegs.Get("duration")
  Dim text As String = duration.Get("text")
  Dim value As Int = duration.Get("value")
  Dim distance As Map = collegs.Get("distance")
'  Log("durata="& distance)
  Dim text As String = distance.Get("text")
  Dim value As Int = distance.Get("value")
  Dim end_location As Map = collegs.Get("end_location")
  Dim lng As Double = end_location.Get("lng")
  Dim lat As Double = end_location.Get("lat")
  Dim start_address As String = collegs.Get("start_address")
  Dim end_address As String = collegs.Get("end_address")
  Dim start_location As Map = collegs.Get("start_location")
  Dim lng As Double = start_location.Get("lng")
  Dim lat As Double = start_location.Get("lat")
  Dim via_waypoint As List = collegs.Get("via_waypoint")
  Dim steps As List = collegs.Get("steps")
  For Each colsteps As Map In steps
  Dim html_instructions As String = colsteps.Get("html_instructions")
  Dim duration As Map = colsteps.Get("duration")
  Dim text As String = duration.Get("text")
  Dim value As Int = duration.Get("value")
  Dim distance As Map = colsteps.Get("distance")
  Dim text As String = distance.Get("text")
  Dim value As Int = distance.Get("value")
  Dim end_location As Map = colsteps.Get("end_location")
  Dim lng As Double = end_location.Get("lng")
  Dim lat As Double = end_location.Get("lat")
  Dim polyline As Map = colsteps.Get("polyline")
  Dim points As String = polyline.Get("points")
  Dim start_location As Map = colsteps.Get("start_location")
  Dim lng As Double = start_location.Get("lng")
  Dim lat As Double = start_location.Get("lat")
  Dim travel_mode As String = colsteps.Get("travel_mode")
  Next
Next
Dim warnings As List = colroutes.Get("warnings")
For Each colwarnings As String In warnings
Next
Dim overview_polyline As Map = colroutes.Get("overview_polyline")
Dim points As String = overview_polyline.Get("points")
Next
Log("durata="& distance)
End Sub
The link i used was: http://maps.googleapis.com/maps/api/directions/json?origin=Calugareni 12, Oradea, Romania&destination=Moldovei, Oradea, Romania&mode=walking&Language=ro-RO

Now i need to get data from the map. So, how can i get from the map first distance from legs, or second leg duration, for example ?
Can you help me ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You can rewrite your code like this... replacing foreach with a for loop.
Do with your loop what you need... i=0 first item
i=10 11th item

B4X:
Sub rezolvaJson(jsonfile As String)
    Dim parser As JSONParser
    parser.Initialize(jsonfile)
    Dim root As Map = parser.NextObject
    Dim status As String = root.Get("status")
    Dim routes As List = root.Get("routes")
    For Each colroutes As Map In routes
        Dim summary As String = colroutes.Get("summary")
        Dim bounds As Map = colroutes.Get("bounds")
        Dim southwest As Map = bounds.Get("southwest")
        Dim lng As Double = southwest.Get("lng")
        Dim lat As Double = southwest.Get("lat")
        Dim northeast As Map = bounds.Get("northeast")
        Dim lng As Double = northeast.Get("lng")
        Dim lat As Double = northeast.Get("lat")
        Dim copyrights As String = colroutes.Get("copyrights")
        Dim waypoint_order As List = colroutes.Get("waypoint_order")
        Dim legs As List = colroutes.Get("legs")
        For i = 0 To legs.Size -1
            Dim collegs As Map = legs.Get(i)
            Dim duration As Map = collegs.Get("duration")
            Dim text As String = duration.Get("text")
            Dim value As Int = duration.Get("value")
            Dim distance As Map = collegs.Get("distance")
            Log(i&": durata="& distance)
            Dim text As String = distance.Get("text")
            Dim value As Int = distance.Get("value")
            Dim end_location As Map = collegs.Get("end_location")
            Dim lng As Double = end_location.Get("lng")
            Dim lat As Double = end_location.Get("lat")
            Dim start_address As String = collegs.Get("start_address")
            Dim end_address As String = collegs.Get("end_address")
            Dim start_location As Map = collegs.Get("start_location")
            Dim lng As Double = start_location.Get("lng")
            Dim lat As Double = start_location.Get("lat")
            Dim via_waypoint As List = collegs.Get("via_waypoint")
            Dim steps As List = collegs.Get("steps")
            For Each colsteps As Map In steps
                Dim html_instructions As String = colsteps.Get("html_instructions")
                Dim duration As Map = colsteps.Get("duration")
                Dim text As String = duration.Get("text")
                Dim value As Int = duration.Get("value")
                Dim distance As Map = colsteps.Get("distance")
                Dim text As String = distance.Get("text")
                Dim value As Int = distance.Get("value")
                Dim end_location As Map = colsteps.Get("end_location")
                Dim lng As Double = end_location.Get("lng")
                Dim lat As Double = end_location.Get("lat")
                Dim polyline As Map = colsteps.Get("polyline")
                Dim points As String = polyline.Get("points")
                Dim start_location As Map = colsteps.Get("start_location")
                Dim lng As Double = start_location.Get("lng")
                Dim lat As Double = start_location.Get("lat")
                Dim travel_mode As String = colsteps.Get("travel_mode")
            Next
        Next
        Dim warnings As List = colroutes.Get("warnings")
        For Each colwarnings As String In warnings
        Next
        Dim overview_polyline As Map = colroutes.Get("overview_polyline")
        Dim points As String = overview_polyline.Get("points")
    Next
    Log("durata="& distance)
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Can this function be use to get each point to draw a polyline... tnx
no as you can read in the google documentation. You just have two points (coordinates) which you could connect (draw a line), but this line does not fit the showed ROAD

Maybe there is an API for business for this but i believe it will be not free
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
no as you can read in the google documentation. You just have two points (coordinates) which you could connect (draw a line), but this line does not fit the showed ROAD

Maybe there is an API for business for this but i believe it will be not free

Are you sure there isnt an encoded polyline in the result somewhere?
Maybe a number of encoded polylines - one for each stage of the directions.

If so then that (or those) encoded polyline(s) can be decoded and a drawn on a map.
https://www.b4x.com/android/forum/threads/mapsdecodepolyline.31499/
 
Upvote 0
Top