Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

yay

  • Loading branch information...
commit c92a2ce3e1cfdb212d65fadf12104702569c011a 0 parents
@codepo8 authored
Showing with 261 additions and 0 deletions.
  1. 0  REAMDE
  2. +125 −0 index.html
  3. +136 −0 yqlgeo.js
0  REAMDE
No changes.
125 index.html
@@ -0,0 +1,125 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4+01//EN" "http://www+w3+org/TR/html4/strict+dtd">
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>YQL Geo Library - get all your geo needs in JavaScript - geolocation, reverse geocoding, content analysis</title>
+ <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.8.0/build/reset-fonts-grids/reset-fonts-grids.css&2.8.0/build/base/base-min.css">
+<style type="text/css" media="screen">
+ body,html{background:#999;color:#000;}
+ #doc{background:#fff;border:1em solid #fff;}
+ h1,h2,h3{font-family:calibri,sans-serif;clear:both;}
+ a{color:#369;}
+ pre{border:1px solid #999;background:#eee;padding:.5em;clear:both;margin:1em 0;}
+ h1{margin:0 0 .5em 0;}
+ button{float:right;margin:.5em;}
+ h1{color:#393;font-size:200%;}
+ #ft p{color:#ccc;margin:2em 0;text-align:right;font-size:80%;}
+ #ft p a{color:#999;}
+
+</style>
+</head>
+<body class="yui-skin-sam">
+<div id="doc" class="yui-t7">
+
+ <div id="hd" role="banner">
+ <h1>YQL Geo Library - get all your geo needs in JavaScript - geolocation, reverse geocoding, content analysis</h1>
+ </div>
+ <div id="bd" role="main">
+ <p>As an aid for the Georgia Tech Hack 2010 and as a follow-up <a href="http://www.slideshare.net/cheilmann/introduction-to-geo-hacking-with-amongst-others-yahoo-technology-3388374">to my talk about geolocation</a> here is a simple JavaScript library that answers most of your geo questions. It wraps the following services in a simple interface:</p>
+ <ul>
+ <li><a href="http://developer.yahoo.com/geo/placemaker">Yahoo Placemaker</a></li>
+ <li><a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo GeoPlanet</a></li>
+ <li><a href="http://jsonip.appspot.com">jsonip.appspot.com</a></li>
+ <li><a href="http://iplocationtools.com/ip_location_api.php">IP location tools</a></li>
+ <li><a href="http://dev.w3.org/geo/api/spec-source.html">W3C Geo location</a></li>
+ <li><a href="http://www.flickr.com/services/api/flickr.places.findByLatLon.html">Flickr.places.findByLatLon</a></li>
+ </ul>
+ <h2>Usage</h2>
+ <p>To use the geo library, simply include it in your document. There is one simple method to use which is:</p>
+<pre><code>yqlgeo.get(<em>what</em>,<em>callback</em>)</code></pre>
+ <dl>
+ <dt><em>what</em></dt>
+ <dd>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.</dd>
+ <dt><em>callback</em></dt>
+ <dd>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 <code>error</code> property.</dd>
+ </dl>
+
+ <p>Check out the different use cases of the library below:</p>
+
+ <h2>Getting the geographical location from a text</h2>
+ <p>You can send an arbitrary text to the <code>get()</code> method to find the geographical information in it:</p>
+
+<pre><code>yqlgeo.get('paris,fr',function(o){
+ alert(o.place.name+' ('+
+ o.place.centroid.latitude+','+
+ o.place.centroid.longitude+
+ ')');
+})</code></pre>
+<button onclick="yqlgeo.get('paris,fr',function(o){alert(o.place.name+' ('+o.place.centroid.latitude+','+o.place.centroid.longitude+')');})">Try this out</button>
+
+ <h2>Getting the location information from lat/lon</h2>
+
+ <p>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:</p>
+
+<pre><code>yqlgeo.get(33.748,-84.393,function(o){
+ alert(o.place.name + ',' + o.place.country.content);
+})</code></pre>
+<button onclick="yqlgeo.get(33.748,-84.393,function(o){alert(o.place.name + ',' + o.place.country.content);})">Try this out</button>
+
+<pre><code>yqlgeo.get([33.748,-84.393],function(o){
+ alert(o.place.name + ',' + o.place.country.content);
+})</code></pre>
+<button onclick="yqlgeo.get([33.748,-84.393],function(o){alert(o.place.name + ',' + o.place.country.content);})">Try this out</button>
+
+<h2>Get all the geo locations from a certain URL</h2>
+
+<p>You can scrape a certain document at a URL for geographical locations:</p>
+
+<pre><code>yqlgeo.get('http://icant.co.uk',function(o){
+ var out = '';
+ var all = o.place.length;
+ for(var i=0;i&lt;all;i++){
+ out+=o.place[i].name+'\n';
+ };
+ alert(out);
+});</code></pre>
+<button onclick="yqlgeo.get('http://icant.co.uk',function(o){var out='';var all =o.place.length;for(var i=0;i<all;i++){out+=o.place[i].name+'\n'};alert(out)}); ">Try this out</button>
+
+<h2>Get the place from an IP number</h2>
+
+<pre><code>yqlgeo.get('217.12.14.240',function(o){
+ alert(o.place.name + ',' + o.place.country.content +
+ ' (' + o.place.centroid.latitude + ',' +
+ o.place.centroid.longitude + ')'
+ );
+});
+</code></pre>
+<button onclick="yqlgeo.get('217.12.14.240',function(o){alert(o.place.name + ',' +o.place.country.content + ' ('+o.place.centroid.latitude+','+o.place.centroid.longitude+')')});">Try this out</button>
+
+<h2>Find the user (using the W3C geolocation API and IP as a fallback)</h2>
+
+<pre><code>yqlgeo.get('visitor',function(o){
+ alert(o.place.name + ',' + o.place.country.content +
+ ' (' + o.place.centroid.latitude + ',' +
+ o.place.centroid.longitude + ')'
+ );
+});
+</code></pre>
+<button onclick="yqlgeo.get('visitor',function(o){alert(o.place.name + ',' +o.place.country.content + ' ('+o.place.centroid.latitude+','+o.place.centroid.longitude+')')});">Try this out</button>
+
+<h2>Download the library</h2>
+
+<p>You can get the library directly <a href="yqlgeo.js">from here by saving this link</a> or by <a href="">getting the source code from GitHub</a>.</p>
+
+ </div>
+ <div id="ft" role="contentinfo">
+ <p>Written by <a href="http://wait-till-i.com">Chris Heilmann</a> using <a href="http://developer.yahoo.com/yql">YQL</a>.</p>
+ </div>
+
+</div>
+
+<script src="yqlgeo.js"></script>
+
+</body>
+</html>
+
136 yqlgeo.js
@@ -0,0 +1,136 @@
+/*
+ YQL Geo library by Christian Heilmann
+ Homepage: http://isithackday.com/geo/yql-geo-library
+ Copyright (c)2010 Christian Heilmann
+ Code licensed under the BSD License:
+ http://wait-till-i.com/license.txt
+*/
+var yqlgeo = function(){
+ var callback;
+ function get(){
+ var args = arguments;
+ for(var i=0;i<args.length;i++){
+ if(typeof args[i] === 'function'){
+ callback = args[i];
+ }
+ }
+ if(args[0] === 'visitor'){getVisitor();}
+ if(typeof args[0] === 'string' && args[0] != 'visitor'){
+ if(args[0]){
+ if(/^http:\/\/.*/.test(args[0])){
+ getFromURL(args[0]);
+ } else if(/^[\d+\.?]+$/.test(args[0])){
+ getFromIP(args[0]);
+ } else {
+ getFromText(args[0]);
+ }
+ }
+ }
+ var lat = args[0];
+ var lon = args[1];
+ if(typeof lat.join !== undefined && args[0][1]){
+ lat = args[0][0];
+ lon = args[0][1];
+ };
+ if(isFinite(lat) && isFinite(lon)){
+ if(lat > -90 && lat < 90 &&
+ lon > -180 && lon < 180){
+ getFromLatLon(lat,lon);
+ }
+ }
+ }
+ function getVisitor(){
+ if(navigator.geolocation){
+ navigator.geolocation.getCurrentPosition(
+ function(position){
+ getFromLatLon(position.coords.latitude,
+ position.coords.longitude);
+ },
+ function(error){
+ retrieveip();
+ }
+ );
+ } else{
+ retrieveip();
+ }
+ };
+
+ function getFromIP(ip){
+ var yql = 'select * from geo.places where woeid in ('+
+ 'select place.woeid from flickr.places where (lat,lon) in('+
+ 'select Latitude,Longitude from ip.location'+
+ ' where ip="'+ip+'"))';
+ load(yql,'yqlgeo.retrieved');
+ };
+
+ function retrieveip(){
+ jsonp('http://jsonip.appspot.com/?callback=yqlgeo.ipin');
+ };
+
+ function ipin(o){
+ getFromIP(o.ip);
+ };
+
+ function getFromLatLon(lat,lon){
+ var yql = 'select * from geo.places where woeid in ('+
+ 'select place.woeid from flickr.places where lat='+
+ lat + ' and lon=' + lon + ')';
+ load(yql,'yqlgeo.retrieved');
+ };
+
+ function getFromURL(url){
+ var yql = 'select * from geo.places where woeid in ('+
+ 'select match.place.woeId from geo.placemaker where '+
+ 'documentURL="' + url + '" and '+
+ 'documentType="text/html" and appid="")';
+ load(yql,'yqlgeo.retrieved');
+ }
+
+ function getFromText(text){
+ var yql = 'select * from geo.places where woeid in ('+
+ 'select match.place.woeId from geo.placemaker where'+
+ ' documentContent = "' + text + '" and '+
+ 'documentType="text/plain" and appid = "")';
+ load(yql,'yqlgeo.retrieved');
+ };
+
+ function jsonp(src){
+ if(document.getElementById('yqlgeodata')){
+ var old = document.getElementById('yqlgeodata');
+ old.parentNode.removeChild(old);
+ }
+ var head = document.getElementsByTagName('head')[0];
+ var s = document.createElement('script');
+ s.setAttribute('id','yqlgeodata');
+ s.setAttribute('src',src);
+ head.appendChild(s);
+ };
+
+ function load(yql,cb){
+ if(document.getElementById('yqlgeodata')){
+ var old = document.getElementById('yqlgeodata');
+ old.parentNode.removeChild(old);
+ }
+ var src = 'http://query.yahooapis.com/v1/public/yql?q='+
+ encodeURIComponent(yql) + '&format=json&callback=' + cb + '&'+
+ 'env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
+ var head = document.getElementsByTagName('head')[0];
+ var s = document.createElement('script');
+ s.setAttribute('id','yqlgeodata');
+ s.setAttribute('src',src);
+ head.appendChild(s);
+ };
+
+ function retrieved(o){
+ if(o.query.results){
+ callback(o.query.results);
+ } else {
+ callback({error:o.query});
+ }
+ };
+ return {
+ get:get,
+ retrieved:retrieved,
+ ipin:ipin
+ };
+}();

1 comment on commit c92a2ce

@nikolay

The filename has a typo.

Please sign in to comment.
Something went wrong with that request. Please try again.