01 Nov
Posted by admin as FileMaker, Google, Lasso Professional
I know this has been an issue for a lot of developers out there, both in the FileMaker realm and the Web application realm.
The solution I came up with still uses Google Maps, it’s easier to work with, and it seems to be more reliable than scrapping the Google Maps page itself.
First, lets look at the URL we are working with (I am using two test addresses you MAY be familiar with):
http://maps.google.com?saddr=1+Infinite+Loop+95014&daddr=one+microsoft+way+98052&output=kml
Here is a simple Google Maps URL with one exception. It’s the exception that makes this work. Output=kml. KML is the XML like structure that Google Earth uses to pull in directions. If you go to that URL it will prompt you to download the KML file. This is less them optimal, although still can be used. The reason for that is the browser doesn’t know how to handle it.
If we take another approach and use Lasso or PHP or any flavor of middle-ware you would like, we can do some pretty easy parsing with this.
I will use Lasso for this example:
<?LassoScript
//Grab Action Params from URL
Var: ’saddr’ = (Action_Param:’saddr’);
$saddr->(Replace: ‘ ‘, ‘+’);
Var: ‘daddr’ = (Action_Param:’daddr’);
$daddr->(Replace: ‘ ‘, ‘+’);//Load Settings for Communication
Var: ‘url’ = ‘http://maps.google.com/?’;//Transmit to Google
Var: ‘return’ = (Include_url: $url + ‘&saddr=’ + $saddr + ‘&daddr=’ + $daddr + ‘&output=kml’);
Var: ‘xml’ = (xml: $return);//Extract XML Nodes
Var: ‘directions’ = ( XML_extract: -XML=$xml, -XPath=’//*[local-name() = \'name\']/text()’ );Var: ‘distance’ = ( XML_extract: -XML=$xml, -XPath=’//*[local-name() = \'description\']/text()’ );
//Remove the last node
$directions->RemoveLast;//Load first node to variable and remove
Var: ’start’ = $directions->(Get: 1);
$directions->RemoveFirst;?>
Let’s talk about the LassoScript I entered above. The first few chunks are pretty self explanatory. We make sure our GET parameters have no spaces. If they do, they are replaced with a +. Then we load our default URL into a variable.
Now the fun begins! The transmit to Google section has two very important parts. The first, is making sure that our URL ends with, “output=kml”, AND our result is set as XML. Because Google specifies its output as KML, it’s different enough to not be automatically cast as XML.
Now that we’ve returned our result we can start extracting nodes from the structure. Because we cast it to XML, we can use standard X-Path parsing to pull out the nodes we need. In this case, directions and distance!
We build our variables directions and distance with the corresponding XML nodes. That way we can iterate through them later on for output.
The last two chunks are clean up. If you don’t get rid of the first and last lines of the directions, the output doesn’t line up when you reassemble for display.
So now, let’s see the HTML associated with this page:
<body>
<ul>
<li>[Output: $start]</li>
[Iterate: $directions, (Var: 'temp0')]
<li>[Output: $temp0] [Output: Decode_HTML: (String_Replace:(String_Replace:(String_Replace:($distance->(Get: (Loop_Count))), -Find='<![CDATA[',-Replace=''), -Find=']]>’, -Replace=”), -Find=’<br/>’, -Replace=’ ‘)]</li>
[/Iterate]
</ul>
</body>
The HTML above displays the results as an unordered list. This can be very easy to parse or just display. Our first list item is the variable “start”. Remember in our LassoScript we cast the first node into that variable before we removed it. What I am doing is showing it outside of the Iterator. This is the header that will show the From and To addresses before the directions are displayed.
Next we start to iterate through the variables “directions” and “distance”. Because the distance nodes are HTML encoded, I use the Decode_HTML tag to make sure it’s displayed right. I also do some string replaces to clean up the way the distance information is displayed.
Here is my example in action. Please feel free to use it with your solution.
RSS feed for comments on this post · TrackBack URI
Leave a reply