PathOverlay

Nyptop

Active Member
Licensed User
Longtime User
Hi all,

I'm trying to make use of google's directions API but I am having trouble making it work.

An example of the JSON the api may return would be something as follows:
http://maps.googleapis.com/maps/api...=London&destination=Peterborough&sensor=false

Here is my best attempt at sorting the returned JSON:

B4X:
Sub JSONResponse_StreamFinish (Success As Boolean, TaskId As Int)
   Dim ResponseObject As Map
   ResponseObject.Initialize
   
   Dim ResponseString As String
   ResponseString=File.ReadString(File.DirInternalCache, "Directions.json")
   
   Dim JSON As JSONParser
   JSON.Initialize(ResponseString)
   
   ResponseObject = JSON.NextObject
   Dim routes As List
   routes = ResponseObject.Get("routes")
   Dim Map1 As Map
   Map1 = routes.Get(0)
   Dim legs As List
   legs = Map1.Get("legs")
   Dim Map2 As Map
   Map2 = legs.Get(0)
   Dim steps As List
   steps = Map2.Get("steps")
   Dim Map3 As Map
   Dim StartLocation As Map
   Dim EndLocation As Map
      
      For i = 0 To steps.Size - 1
      Map3 = steps.Get(i)
      StartLocation = Map3.Get("start_location")
      EndLocation = Map3.Get("end_location")
      Dim PLat, PLon As Double
      PLat = StartLocation.Get("lat")
      PLon = StartLocation.Get("lon")
      PO1.AddPoint(PLat, PLon)
      Next
      
   PO1.StrokeWidth = 5
   PO1.Alpha = 150
   Mapview1.AddOverlay(PO1)
   ProgressDialogHide
End Sub

Note that PO1 is a path overlay and Map1, Map2 and Map3 are used for the objects with no name.

Cheers,

Neil
 

warwound

Expert
Licensed User
Longtime User
What errors are you getting?

Can you upload a sample B4A project so we can test it ourselves?
(Use File menu Export as ZIP).

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

I fixed a few errors in your project and it seems to process the JSON correctly.
None of the errors i fixed were related to your JSON processing however, your HttpClients needed to be declared as Process objects not Global objects.
And they both needed to be Initialized before using them.
After Initializing the HttpClients i updated your HttpClient callbacks Sub names with the EventName i used to Initialize them.

Then i included the StringUtils library and UrlEncoded the 'From' and 'Destination' values.

The result - still an error.
So i updated your JSON parsing routine to check the 'status' property of the directions, here's the response from Google:

B4X:
{


   "routes" : [],
   "status" : "REQUEST_DENIED"
}

Your JSON parsing wasn't failing but Google are refusing to process your request!

I sent the same request on my desktop computer and saved the result - the request was properly process and status was ok.
I moved the saved result file to your project assets and changed the line that loaded the ResponseString:

B4X:
ResponseString=File.ReadString(File.DirAssets, "Directions2.json")

Zooming out the map i can now see your code has properly parsed the JSON and rendered a path, no tiles are loaded as you've disbaled data connection.
(I actually renabled data but still no tiles loaded - don't have time to look into that right now).

The problem is the sensor parameter in your request, it should be true or false but NOT True!!

https://developers.google.com/maps/documentation/directions/

B4X:
request.InitializeGet("http://maps.googleapis.com/maps/api/directions/json?origin="& From &"&destination="& Destination &"&sensor=false")

Now it all works!

I've attached London.B4A so you can see my edits.

Martin.
 

Attachments

  • London.zip
    3.3 KB · Views: 227
  • london_peterborough.png
    london_peterborough.png
    32.2 KB · Views: 236
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Wow Warwound this is excellent. I do not have any money on a debit card as it stands but I shall endeavour to buy you a beer as soon as I get some money! Thanks again,

Neil
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Neil,

Nice response - thoughtful - respectful (as it was helpful for you). A classic example the community helping others.

Martin is the man for Google and everything maps (and Klaus) - and so much more. He has self studied and learned from the masters. I look forward to the day when I can contract him on a meaningful basis. The advise he gave me uncovered many areas of my project I had not even considered or known of. He is one of a special few (OF many) that really contribute to this cause.
Just look for his many posts - and projects.

Not to say Martin can take all the blame for this helpfulness...

the message: Support others when you can...
 
Upvote 0
Top