Hi Paul,
Here is some self contained Powerbuilder code that will call the google distance API and parse the result to give you the distance and duration. I've just presented here as a big block of code, but realistically you would take it and split it into at least two separate functions, one for the http call and one to parse the result. This should give you an idea about how to call the google API from Powerbuilder using Powerscript. This code was written in Powerbuilder 2019R3.
// This is an example of how to call the Google Directions API
// It would probably be in a function that returns the json
string ls_json
HttpClient lnv_HttpClient
integer li_rc
integer li_ResponseStatusCode = 0
string ls_ResponseBody = ''
string ls_ResponseStatusMessage = ''
string ls_url
// Hardcoded as an example, but should be build with latitude/longitude varables and API key
ls_url = 'https://maps.googleapis.com/maps/api/directions/json?origin=50.823189,6.191204&destination=50.776200,6.083800&key=AI...us'
lnv_HttpClient = Create HttpClient
lnv_HttpClient.SetRequestHeader('Accept', 'application/json')
li_rc = lnv_HttpClient.SendRequest('GET', ls_url)
if li_rc = 1 then
// Can check these for a 200 succesful call, but they aren't used at the moment
li_ResponseStatusCode = lnv_HttpClient.GetResponseStatusCode()
ls_ResponseStatusMessage = lnv_HttpClient.GetResponseStatusText()
li_rc = lnv_HttpClient.GetResponseBody(ls_json, EncodingUTF8!)
if li_rc <> 1 then
MessageBox('HttpClient Error', 'Error getting response body for Request. Return Code ' + string(li_rc))
end if
else
MessageBox('HttpClient Error', 'Error calling ' + ls_url + ' - Error Code ' + string(li_rc))
end if
// This is an example of how to parse the directions api json to find the duration and distance
// This example only gets the first leg of the first route, but the json can return multiple routes
// with multiple legs so that should be considered when creating the solution for real.
// It would probably be in a separate function that accepts the json from the http call
// and returns a new object with the distance and duration
string ls_error
long ll_root
jsonparser lnv_jsonparser
string ls_status
string ls_error_message
long ll_routes
long ll_route
long ll_legs
long ll_leg
long ll_distance
long ll_duration
long ll_distance_value
long ll_duration_value
string ls_distance_text
string ls_duration_text
lnv_jsonparser = create jsonparser
ls_error = lnv_jsonparser.LoadString(ls_json)
if Len(Trim(ls_error)) = 0 then
ll_root = lnv_jsonparser.GetRootItem()
ls_status = lnv_jsonparser.GetItemString(ll_root, 'status')
if isNull(ls_status) then ls_status = ''
if ls_status = 'OK' then
ll_routes = lnv_jsonparser.GetItemArray(ll_root, 'routes')
if ll_routes > 0 then
// Get the first route (in real life you would probably interate through and get all routes)
ll_route = lnv_jsonparser.GetChildItem(ll_routes, 1)
ll_legs = lnv_jsonparser.GetItemArray(ll_route, 'legs')
if ll_legs > 0 then
// Get the first leg (in real life you would probably interate through and get all legs)
ll_leg = lnv_jsonparser.GetChildItem(ll_legs, 1)
ll_distance = lnv_jsonparser.GetItemObject(ll_leg, 'distance')
// These are the two distance values you will ultimately need/use
ll_distance_value = lnv_jsonparser.GetItemNumber(ll_distance, 'value')
ls_distance_text = lnv_jsonparser.GetItemString(ll_distance, 'text')
ll_duration = lnv_jsonparser.GetItemObject(ll_leg, 'duration')
// These are the two duration values you will ultimately need/use
ll_duration_value = lnv_jsonparser.GetItemNumber(ll_duration, 'value')
ls_duration_text = lnv_jsonparser.GetItemString(ll_duration, 'text')
MessageBox('API Success', 'Distance = ' + ls_distance_text + ' (' + string(ll_distance_value) + ')' + &
'~r~nDuration = ' + ls_duration_text + ' (' + string(ll_duration_value) + ')')
else
// No legs, so some error has occurred
MessageBox('API Error', 'Error calling the API. No legs returned.')
end if
else
// No routes, so some error has occurred
MessageBox('API Error', 'Error calling the API. No routes returned.')
end if
else
// We didn't get an OK status, so something went wrong
ls_error_message = lnv_jsonparser.GetItemString(ll_root, 'error_message')
if isNull(ls_error_message) then ls_error_message = ''
MessageBox('API Error', 'Error calling the API. Return Status = ' + ls_status + '~r~nError Message: ' + ls_error_message)
end if
else
MessageBox('Parse Error', 'Failed to load error json ' + ls_error)
end if
Paul, as the owner of this thread would you please mark this issue as resolved? Thanks.
Thank you Paul and Daryl.