Thursday, March 11th, 2010
YQL Geo library – all your geo needs in pure JavaScript
<p>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!
Related Content:











Chris, your blog throws a warning about malware in Chromium. When I visit anyway, I get some pdf file downloaded automatically (myreadme.php.pdf) which I deleted on the assumption that it’s malware. Chromium then gives a warning box saying “this site wants to download multiple files to your computer: allow/deny”.
All of which is to say, I think you’ve got some funky shit going down at your blog that you ought to take a look at.
If media temple have a vulnerability nothing in the world is safe anymore… :D
Awesome.
Hello Christian!
Your library is great, became a standard, and I’ve been using it for a long time now!
However, with recent changes to the YQL, it doesn’t work anymore, when you try to get geo-info from IP.
And this is because querying flickr.places from YQL fails now, no matter what. Thus geo.places can’t get a woeid to query.
Please notice that pidgets.geoip returns the latitude and longitude correctly from a given IP.
Can you think of any workarounds? You are the only one I depend on! :)