Mapping RESTful Data
As I mentioned before, Yahoo provides Yahoo Query Language (YQL) in a variety of forms. There is a console that allows you to write a query in a browser and see the XML results on the screen. This is nice for development purposes, but XML isn’t all that “humanly readable.” I’m a big fan of maps to represent data that can be attached to a latitude and longitude. When you think about it, most data actually can be attached to an address, a zip code, a city or some geospatial component. In my last blog, demonstrated how to use YQL and it’s RESTful API to query and display information in a tabular form. Today I’m going to show you how to take that tablular data and represent it in map.
In my ApEx application, I created a region that contains 4 input fields:
You can see that I’m collecting:
- Address (to use for events and traffic)
- Weather Zip (zip code to use for the weather information)
- Event Search (search text for the events)
- Radius (radius from the address to search for events and traffic)
As you can see in the following figure, I displayed the traffic information on the map (pretty cool isn’t it). Below the map, I included the detailed traffic information in a tabular format (not very existing is it). The traffic is within the specified radius of the address on the page.
In the following figure you’ll see that I displayed all of the events within the radius of the specified address that contain the search text specified. Again, below the events, is a tabular listing of the event details.
There are many maps available including Yahoo, Microsoft (Virtual Earth), Google, etc. Yahoo Pipes, which I’ve discussed previously, allows you to consume RESTful (or standard) Web Services. Pipes allows you to consume YQL queries too. I decided to use Yahoo Pipes to consume my YQL queries. The figure below shows the Pipes visual diagram, which I’ll explain below.
Above you’ll see that the diagram starts in the upper right corner with 2 text input values (location and radius). These feed into the “String Builder” which writes a SQL statement that’s used in the YQL query. The query will look like: select * from maps.traffic where location=”loc” and radisu=radius. This is fed into the YQL query, which pases it’s results into rename, which changes item.description to description and item.title to title. The “Location Exrtaction” module will pull the lat/long information from YQL query. Amazingly simple isn’t it! When you run the Pipe, you can see that there are 2 tabs. 1 for the Map (because we used the Location Extractor and a list.
You see that all of the traffic shows up in the lat/long location it belongs on the map. I’m always amazed how easy it is to integrate this data when you have services. You can see above that there are many options available with the Pipe data. You can publish the map on your MyYahoo page or you iGoogle page or get it as an RSS or JSON feeds and more. You can also see that there is an option labeled “Get as a Badge.” As you can see here, you can put the badge in TypePad, Blogger, WordPress, iGoogle or as an Embedded link on your Web page:
When you click on “Embed,” Pipes provides you with a link to copy and paste. Now back to ApEx. How do we take this embedded link and put it in ApEx? Remember that we have 2 bind variables for this Pipe. These will be input variables in the RESTful service. For this Pipe, the link that I copied looks like this:
<script src="http://l.yimg.com/a/i/us/pps/mapbadge_1.1.js">{"pipe_id":"1000b6378cfdcd6dd7c46397c03c08a8","_btype":"map","pipe_params":{"loc":"215 Union Blvd, Lakewood, CO","radius":"50"}}</script>
You can see the 2 bind variables. Remember that in my ApEx application, I had 4 input variables (ApEx items) on my page. The names of my variables are: P2_ADDRESS, P2_ZIP, P2_SEARCH and P2_RADIUS. I created a new HTML region on my page. Then I pasted the above embedded script into the region’s HTML source. The only thing I needed to change was to add in my bind variables. You can see how I changed it below. In PL/SQL, you reference bind variables with a colon in front. For example, you would write something like “where radius = :p2_radius” in PL/SQL. In HTML, the binding uses an ampersand before the variable name and a period at the end. So here’s what my region source looks like now:
<script src=”http://l.yimg.com/a/i/us/pps/mapbadge_1.1.js”>{”pipe_id”:”1000b6378cfdcd6dd7c46397c03c08a8″,”_btype”:”map”,”pipe_params”:{”loc”:”&P2_ADDRESS.”,”radius”:”&P2_RADIUS.”}}</script>
My events map HTML region now looks like this:
<script src=”http://l.yimg.com/a/i/us/pps/mapbadge_1.1.js”>{”pipe_id”:”443aa7ea681f51feb5ff3e24cc71812b”,”_btype”:”map”,”pipe_params”:{”search”:”&P2_SEARCH.”,”loc”:”&P2_ADDRESS.”,”radius”:”&P2_RADIUS.”}}</script>
You can see my bind variables in this region too. You can see what my “Events in Town” Pipe looks like when I run it. Notice the visual diagram (flow chart) here too - very cool!
Let’s take a look at the details behind this Pipe now. Here’s the flow:
This Pipe begins in the top middle of the page with 3 text input variables - search text, location and radius. Again, this feeds building the select statement that feeds into YQL. I then took the results of that query and split it into 2 threads. One thread takes a lot of the information and moves it into a “new_description” field. This is used for all of the events, which are then put into an RSS format, sent into the union, which goes into the pipe’s output. The other thread extras the venue’s zip code and gets the weather for that zip code. Since many events could be in the same zip code, I ran it through a unique filter for unique cities and for all unique cities builds a YQL query for the weather information, which is also unioned into the pipe’s output. So we have a powerful combination of service requests.
Again, I’m hoping to inspire you to go and build a really cool application that you can use in your company or personally. Services are VERY powerful. And of course…if you need help, give us a call!








Recent Comments