ooRexx logo usegeoloc.orx
/* ---------------------------------------------------------------- */
/* Demonstrates the use of the geoLoc class                         */
/*----------------------------------------------------------------- */
/*                                                                  */
/* Originally by Ruurd J. Idenburg                                  */                                                            
/*                                                                  */
/* No copyright, no licence, no guarantees or warrantees, be it     */
/* explicit, implicit or whatever. Usage is totally and completely  */
/* at the users own risk, the author shall not be liable for any    */
/* damages whatsoever, for any reason whatsoever.                   */
/*                                                                  */
/* Please keep this comment block intact when modifying this code   */
/* and add a note with date and a description.                      */
/*                                                                  */
/* ---------------------------------------------------------------- */
/* 2013/12/08 - Initial version                                     */
/* ---------------------------------------------------------------- */

begin = 'Bertram 1, Uithoorn, Nederland'
end = 'Bergeend 29, Uithoorn, Nederland'

say 'Asking Google the walking route from:' begin
say '                                  to:' end
say 'in JSON format'  
begin = begin~changestr(' ','%20')
end = end~changestr(' ','%20')

xhr = .xhr~new('maps.googleapis.com')
--say xhr~rspHeaders~makestring
--say xhr~response
say 'Transforming JSON to an ooRexx Directory'
jsonRoute = .json~new()~fromJson(xhr~response)
--trace i
say 'Retrieving route steps from ooRexx Directory, while storing steps in an Array'
googleSteps = .array~new
routes = jsonRoute['routes']~items
do r=1 to routes
  route = jsonRoute['routes'][r]
  legs = route['legs']~items
  do l=1 to legs
    leg = route['legs'][l]
    steps = leg['steps']~items
    do s=1 to steps
      step = leg['steps'][s]
      stepDist = (step['distance']['value']/1000)~format(,3) -- in meters
      stepStart = step['start_location']['lat']~format(,7)','step['start_location']['lng']~format(,7)
      stepEnd = step['end_location']['lat']~format(,7)','step['end_location']['lng']~format(,7)
      stepPoly = step['polyline']['points']
      stepItem = stepDist '-' stepStart '-' stepEnd '-' stepPoly
      say stepItem
say "Comparing Google's route distance with ours. Ours is probably shorter,"
say "because Google also uses it's encoded polyline points in the calculation"
say "and we don't. But our 2 calculations should be the same, as the geoPath"
say "'distance' method uses the geoLoc 'distanceFrom' method."
say "Using 'decodeGString.rex' to decode the encoded polyline into latitude/longitude pairs"
say "in each step of the route directions should yield a distance that should be equal to"
say "Google's distance."
googleDist = 0
ourDist = 0
loopDist = 0~format(,3)
path = .geoPath~new
do s=1 to googleSteps~items
  parse value googleSteps[s] with dist ' - ' loc1 ' - ' loc2 ' - ' poly
  googleDist += dist
  parse var loc1 lat ',' lon
  loc1 = .geoLoc~new(lat,lon)
  parse var loc2 lat ',' lon
  loc2 = .geoLoc~new(lat,lon)
  tempDist = loc1~distanceFrom(loc2)
  ourDist += tempDist
  say loopDist '-' loc1~latitude','loc1~longitude
  loopDist = tempDist
say tempDist '-' loc2~latitude','loc2~longitude
pathDist = path~distance -- should be equal to ourDist
say 'Google:' googleDist '- We(should be equal to next):' ourDist '- Us via geoPath:' pathDist

::requires 'geoloc.cls'
::requires 'xhr.cls'
::requires 'json.cls'
If you feel inclined to make corrections, suggestions etc., please mail me any.
All content © Ruurd Idenburg, 2007–2018, except where marked otherwise. All rights reserved. This page is primarily for non-commercial use only. The Idenburg website records no personal information and sets no ‘cookies’. This site is hosted on a VPS(Virtual Private System) rented from CloudVPS, a Dutch company, falling under Dutch (privacy) laws.

This page updated on Thu, 28 Apr 2016 14:59:56 +0200 by Ruurd Idenburg.