Skip to content

Instantly share code, notes, and snippets.

@roc
Created May 2, 2011 18:17
Show Gist options
  • Save roc/952075 to your computer and use it in GitHub Desktop.
Save roc/952075 to your computer and use it in GitHub Desktop.
Super quick, slightly dumb ajax geoCoding script.
//BAM - it's dependent on jquery atm
//<script src="ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js">
//</script>
var $someDivForOutput = $("section#divYeahExactly");
var cityList =
{
geoCodeLocation : function(loc)
{
$.ajax({
url: 'http://maps.google.com/maps/geo?q='+loc,
key: 'YOURKEYHERE',
dataType: 'jsonp',
cache: false,
success: function (data, loc) {
//Ensure we have a 200 code
if(data.Status.code == 200) {
//build e.g. some geoJson point data?
var point = data.Placemark[0].Point;
var name = data.name;
var r = {
"id":name,
"type":"Feature",
"geometry": {
"type": "Point",
"coordinates": [
point.coordinates[0],
point.coordinates[1]
]
}
};
//log it
console.log(r);
//or output it
$someDivForOutput.append(JSON.stringify(r) + ",");
} else {
console.log("ack google didn't like what you sent them. Returned code: " + data.Status.code, data);
}
}
});
},
//there's probably a smarter && cleaner way of doing this, but it works
//if you have a large list of names like 'new york, london, paris' etc, this may be useful
geoEncodeList : function(list)
{
var lObj = {};
lObj.init = function(){
lObj.ping();
};
lObj.ping = function(){
if (list.length){
cityList.geoCodeLocation(list[list.length-1]);
setTimeout( function(){
lObj.init();
}, 700); //could probably run quicker than this, but ensures you don't get 'too many requests' throttling problems.
//log the list console.log( list.pop() );
//or just pop it
list.pop();
return;
}
//console.log('at the end of the list');
};
lObj.init();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment