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.
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
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
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
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
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
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