Thursday, March 11th, 2010

YQL Geo library – all your geo needs in pure JavaScript

Category: Geo, JavaScript, Library

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

javascript
< view plain text >
  1. yqlgeo.get(
  2.   'paris,fr',
  3.    function(o){
  4.      console.log(o);
  5.   }
  6. )

Will get you:

javascript
< view plain text >
  1. "place":{
  2.   "lang":"en-US",
  3.   "uri":"http://where.yahooapis.com/v1/place/615702",
  4.   "woeid":"615702",
  5.   "placeTypeName":{
  6.     "code":"7",
  7.     "content":"Town"
  8.   },
  9.   "name":"Paris",
  10.   "country":{
  11.     "code":"FR",
  12.     "type":"Country",
  13.     "content":"France"
  14.   },
  15.   "admin1":{
  16.     "code":"",
  17.     "type":"Region",
  18.     "content":"Ile-de-France"
  19.   },
  20.   "admin2":{
  21.     "code":"FR-75",
  22.     "type":"Department",
  23.     "content":"Paris"
  24.   },
  25.   "admin3":null,
  26.   "locality1":{
  27.     "type":"Town",
  28.     "content":"Paris"
  29.   },
  30.   "locality2":null,
  31.   "postal":null,
  32.   "centroid":{
  33.     "latitude":"48.856918",
  34.     "longitude":"2.341210"
  35.   },
  36.   "boundingBox":{
  37.     "southWest":{
  38.       "latitude":"48.658291",
  39.       "longitude":"2.086790"
  40.     },
  41.     "northEast":{
  42.       "latitude":"49.046940",
  43.       "longitude":"2.637910"  
  44.     }
  45.   }
  46. }

Other uses:

This gets the name and the country of a lat/lon pair:

javascript
< view plain text >
  1. yqlgeo.get(33.748,-84.393,function(o){
  2.   alert(o.place.name + ',' + o.place.country.content);
  3. })

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)

javascript
< view plain text >
  1. yqlgeo.get('visitor',function(o){
  2.   alert(o.place.name + ',' + o.place.country.content +  
  3.         ' (' + o.place.centroid.latitude + ',' +
  4.                o.place.centroid.longitude + ')'
  5.         );
  6. });

Read all about it on my blog and enjoy!

Related Content:

Posted by Chris Heilmann at 10:04 am
4 Comments

++++-
4.7 rating from 25 votes

4 Comments »

Comments feed TrackBack URI

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.

Comment by llimllib — March 11, 2010

If media temple have a vulnerability nothing in the world is safe anymore… :D

Comment by gabel — March 11, 2010

Awesome.

Comment by Skilldrick — March 15, 2010

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! :)

Comment by grvulture — October 11, 2011

Leave a comment

You must be logged in to post a comment.