YQL Geo library – all your geo needs in pure JavaScript
I just finished doing some talks on geo hacking (slides are available here) and how to use some of the Geo technologies Yahoo and Google provide as part of a University gig in Atlanta.
As a lot of the students liked the idea of APIs like GeoPlanet and Placemaker but had a hard time getting their head around them I thought it a good idea to build a small JavaScript library that does the job for them.
I give you the YQL Geo library (and its source on GitHub). Using this library you can do the following:
- Detecting the visitor’s location with the W3C geo API and with IP as a fallback
- Find geo location from text
- Find location from lat/lon pair
- Find locations in a certain web document (by URL)
- Get the location for a certain IP number
And all of that in pure JavaScript. For example:
-
yqlgeo.get(
-
‘paris,fr’,
-
function(o){
-
console.log(o);
-
}
-
)
Will get you:
-
“place”:{
-
“lang”:“en-US”,
-
“uri”:“http://where.yahooapis.com/v1/place/615702″,
-
“woeid”:“615702″,
-
“placeTypeName”:{
-
“code”:“7″,
-
“content”:“Town”
-
},
-
“name”:“Paris”,
-
“country”:{
-
“code”:“FR”,
-
“type”:“Country”,
-
“content”:“France”
-
},
-
“admin1″:{
-
“code”:“”,
-
“type”:“Region”,
-
“content”:“Ile-de-France”
-
},
-
“admin2″:{
-
“code”:“FR-75″,
-
“type”:“Department”,
-
“content”:“Paris”
-
},
-
“admin3″:null,
-
“locality1″:{
-
“type”:“Town”,
-
“content”:“Paris”
-
},
-
“locality2″:null,
-
“postal”:null,
-
“centroid”:{
-
“latitude”:“48.856918″,
-
“longitude”:“2.341210″
-
},
-
“boundingBox”:{
-
“southWest”:{
-
“latitude”:“48.658291″,
-
“longitude”:“2.086790″
-
},
-
“northEast”:{
-
“latitude”:“49.046940″,
-
“longitude”:“2.637910″
-
}
-
}
-
}
Other uses:
This gets the name and the country of a lat/lon pair:
-
yqlgeo.get(33.748,-84.393,function(o){
-
alert(o.place.name + ‘,’ + o.place.country.content);
-
})
This finds the visitor’s location (on W3C geo API enabled browsers it asks them to share it – otherwise it detects the IP and locates this one on the planet)
-
yqlgeo.get(‘visitor’,function(o){
-
alert(o.place.name + ‘,’ + o.place.country.content +
-
‘ (‘ + o.place.centroid.latitude + ‘,’ +
-
o.place.centroid.longitude + ‘)’
-
);
-
});
Read all about it on my blog and enjoy!





