28 |

As an aid for the Georgia Tech Hack 2010 and as a follow-up to my talk about geolocation here is a simple JavaScript library that answers most of your geo questions. It wraps the following services in a simple interface:

29 | 37 |

Usage

38 |

To use the geo library, simply include it in your document. There is one simple method to use which is:

39 |
yqlgeo.get(what,callback)
40 |
41 |
what
42 |
is the thing you want to analyse - this could be the URL to a web document, a text, an IP, a pair of latitude and longitude information or "visitor" to detect the geographic location of the current visitor.
43 |
callback
44 |
is the callback method that will be called when there was a succesful retrieval of information. The data comes back as an object - if there was an error retrieving the information the data will be wrapped in an error property.
45 |
46 | 47 |

Check out the different use cases of the library below:

48 | 49 |

Getting the geographical location from a text

50 |

You can send an arbitrary text to the get() method to find the geographical information in it:

51 | 52 |
yqlgeo.get('paris,fr',function(o){
 53 |   alert(o.place.name+' ('+
 54 |     o.place.centroid.latitude+','+
 55 |     o.place.centroid.longitude+
 56 |   ')');
 57 | })
58 | 59 | 60 |

Getting the location information from lat/lon

61 | 62 |

You can get all kind of data from a latitude and longitude pair. You can either send them in as two parameters or as an array:

63 | 64 |
yqlgeo.get(33.748,-84.393,function(o){
 65 |   alert(o.place.name + ',' + o.place.country.content);
 66 | })
67 | 68 | 69 |
yqlgeo.get([33.748,-84.393],function(o){
 70 |   alert(o.place.name + ',' + o.place.country.content);
 71 | })
72 | 73 | 74 |

Get all the geo locations from a certain URL

75 | 76 |

You can scrape a certain document at a URL for geographical locations:

77 | 78 |
yqlgeo.get('http://icant.co.uk',function(o){
 79 |   var out = '';
 80 |   var all = o.place.length;
 81 |   for(var i=0;i<all;i++){
 82 |     out+=o.place[i].name+'\n';
 83 |   };
 84 |   alert(out);
 85 | });
86 | 87 | 88 |

Get the place from an IP number

89 | 90 |
yqlgeo.get('217.12.14.240',function(o){
 91 |   alert(o.place.name + ',' + o.place.country.content +  
 92 |         ' (' + o.place.centroid.latitude + ',' +
 93 |                o.place.centroid.longitude + ')'
 94 |         );
 95 | });
 96 | 
97 | 98 | 99 |

Find the user (using the W3C geolocation API and IP as a fallback)

100 | 101 |
yqlgeo.get('visitor',function(o){
102 |   if(o.error){
103 |     alert('No location found for user :('); // some IPs are not in the DB :(
104 |   } else {
105 |     alert(o.place.name + ',' + o.place.country.content +  
106 |           ' (' + o.place.centroid.latitude + ',' +
107 |                  o.place.centroid.longitude + ')'
108 |           );
109 |   }
110 | });
111 | 
112 | 113 | 114 |

Download the library

115 | 116 |

You can get the library directly from here by saving this link or by getting the source code from GitHub.

117 | 118 |