I have long wanted one geocoding solution that was able to handle UK addresses. Although many solutions exist, I have yet to find one API that can handle all addresses; well tonight I stumbled across a solution, one that as far as I can see is legit and simple - using Yahoo Pipes.
The full pipe can be viewed here, but the power of it comes by utilising xmlhttprequest and JSON output; in otherwords your own geocoding API. So here are the steps I took, I am sure you could probably streamline it even further.
1) Create a simple xml json file and pop on your server (or use mine mine).
2) In Pipes, create a Fetch Feed Data module pointing to this xml json file.
3) Add a Location Input module.
4) Use a regex module to replace the lat,lon etc xml node content objects with the location module output.
5) Output as JSON and there you have it, your own geocding API that works with UK addresses.
Here is the javascript code I use to query an address and return the lat/lon using Pipes as the engine asynchronously:
function geocode(address){
var addr = encodeURIComponent(address)
req = new XMLHttpRequest ();
req.onreadystatechange = getaddressResponse;
req.open("GET","http://pipes.yahoo.com/pipes/pipe.run?location="
+addr+"&_id=omPLHX3y2xGrjcwzdbq02Q&_run=1&_render=json",true);
req.send(null);
}
function getaddressResponse(){
if (req.readyState == 4) {
if (req.status == 200)
{
var results = eval("(" + req.responseText + ")");
var resultset = results.value.items[0];
var lat = resultset.lat; //eg returns "56.575849"
var lon = resultset.lon; //eg returns "-3.231635"
var street = resultset.street; //eg returns "Mountain Street"
var city = resultset.city; //eg returns "New York"
var state = resultset.state; //eg returns "CA"
var postal = resultset.postal; //eg returns "94104"
var country = resultset.country; //eg returns "United Sates"
var quality = resultset.quality; //eg returns "40"
}
}
}
You can add &_callback=insert-function-name to the pipes url for cross-domain calls, but you know I have NO IDEA what the hell that means or how you use it.
Not too sure how long Yahoo will allow UK addresses to pass through the Pipes engine, will have to wait and see……
technorati tags:geocode, uk, pipes