Make your own geocoder API (inc UK)

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:, ,

3 Comments so far

  1. John Walker on June 15th, 2007

    Hi …er Tone?

    Thanks to the Royal Mail (or Royal Pain, depending on your stance)
    free international geocoding for UK addresses has been tricky. Tom
    Anthony has a Googlesearch solution but I haven’t been able to
    translate it into a server version. However, have managed to do so
    with your URL. Here it is. A bit quick and dirty but it works. Note
    that Yahoo is a bit finicky with the translation and it might take
    a few combinations of street/city/region/country/pc to get one that
    returns a lat, lng. So passing an array of these to the routine so
    it can carry out variations may work better.

    function getLatLng($addr) {
    Global $arr;
    $arr=array(); // reset $arr
    $URL=”http://pipes.yahoo.com/pipes/pipe.run?location=”.urlencode($addr).
    “&_id=omPLHX3y2xGrjcwzdbq02Q&_run=1&_render=json”;
    $addressData = file_get_contents($URL);
    preg_replace_callback(”/\”(lat|lon)\”:\”(.*?)\”/six”,
    “JSON_decode”,$addressData);
    return $arr;
    }
    // callback function for getLatLng function is called twice:
    // when a match is found for “lat”:”nnnnnn” and “lon”:”nnnn”
    // (you may have a better way of unraveling the JSON data)

    function JSON_decode($m) {
    Global $arr;
    $arr[$m[1]]=$m[2]; //
    }
    #########################
    list($lat,$lng)=getLatLng($someaddress);
    ########################

  2. nijel on March 24th, 2008

    This is pretty cool - I’ve been looking for a way to geocode quickly and Pipes seems like a great way to go.

    How did you get the javascript functions above to work on your site? I’m running into security problems with the XMLHttpRequest accessing Yahoo.

    Lela

  3. symptoms fibroid on August 3rd, 2008

    treatment fibroid tumors fibroid

Leave a Reply