nextwebgen.com

The Next Generation Web Now

YQL Geo library – all your geo needs in pure JavaScript

Filed under: Uncategorized — Chris Heilmann at 8:04 am on Thursday, March 11, 2010

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:

  1. yqlgeo.get(
  2.   ‘paris,fr’,
  3.    function(o){
  4.      console.log(o);
  5.   }
  6. )

Will get you:

JAVASCRIPT:

  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:

  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:

  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!

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blogmarks
  • del.icio.us
  • De.lirio.us
  • digg
  • NewsVine
  • YahooMyWeb

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>