Skip to content

Instantly share code, notes, and snippets.

@mager
Created October 15, 2010 22:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mager/629056 to your computer and use it in GitHub Desktop.
Save mager/629056 to your computer and use it in GitHub Desktop.
This is an example of SimpleGeo's GeoIP functionality for getting the user's location based on IP address. This example uses Polymaps.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var simplegeo=(function(b,d){var c=(function(k,g){function e(){return(new Date).getTime()}var m=k.document;var n=e(),i=/=\?(&|$)/,j=/(\?|&)_=.*?(&|$)/,l=/\?/,h=/%20/g,f=Object.prototype.toString,o={isFunction:function(p){return f.call(p)==="[object Function]"},isArray:function(p){return f.call(p)==="[object Array]"},each:function(s,w,r){var q,t=0,u=s.length,p=u===g||o.isFunction(s);if(r){if(p){for(q in s){if(w.apply(s[q],r)===false){break}}}else{for(;t<u;){if(w.apply(s[t++],r)===false){break}}}}else{if(p){for(q in s){if(w.call(s[q],q,s[q])===false){break}}}else{for(var v=s[0];t<u&&w.call(v,t,v)!==false;v=s[++t]){}}}return s},param:function(p){var q=[];if(o.isArray(p)||p.jquery){o.each(p,function(){u(this.name,this.value)})}else{for(var t in p){r(t,p[t])}}return q.join("&").replace(h,"+");function r(s,v){if(o.isArray(v)){o.each(v,function(x,w){if(/\[\]$/.test(s)){u(s,w)}else{r(s+"["+(typeof w==="object"||o.isArray(w)?x:"")+"]",w)}})}else{if(v!=null&&typeof v==="object"){o.each(v,function(x,w){r(s+"["+x+"]",w)})}else{u(s,v)}}}function u(s,v){v=o.isFunction(v)?v():v;q[q.length]=encodeURIComponent(s)+"="+encodeURIComponent(v)}},ajax:function(A){var y,t,r,q=A;if(A.data&&typeof A.data!=="string"){A.data=o.param(A.data)}y=A.jsonpCallback||("jsonp"+n++);if(A.data){A.data=(A.data+"").replace(i,"="+y+"$1")}A.url=A.url.replace(i,"="+y+"$1");k[y]=k[y]||function(s){t=s;z();p();k[y]=g;try{delete k[y]}catch(B){}if(x){x.removeChild(w)}};var v=e();var u=A.url.replace(j,"$1_="+v+"$2");A.url=u+((u===A.url)?(l.test(A.url)?"&":"?")+"_="+v:"");A.url+=(l.test(A.url)?"&":"?")+A.data;var x=m.getElementsByTagName("head")[0]||m.documentElement;var w=m.createElement("script");w.src=A.url;x.insertBefore(w,x.firstChild);return g;function z(){if(A.success){A.success.call(q,t,r)}}function p(){if(A.complete){A.complete.call(q,g,r)}}}};return o})(b);var a=(function(i){var e="0.1";var h="api.simplegeo.com";var g="80";var j="http://"+h+":"+g+"/"+e;var f={record:"/records/layer/id.json",records:"/records/layer/ids.json",history:"/records/layer/id/history.json",nearby:"/records/layer/nearby/arg.json",nearbyAddress:"/nearby/address/lat,lon.json",densityDay:"/density/day/lat,lon.json",densityHour:"/density/day/hour/lat,lon.json",contains:"/contains/lat,lon.json",overlaps:"/overlaps/south,west,north,east.json",boundary:"/boundary/id.json",locate:"/locate/ip.json",locateSelf:"/locate.json"};var k=function(l){this.token=l};k.prototype={request:function(m,l,n){l.token=this.token;l=i.param(l)+"&callback=?";i.ajax({url:j+m,dataType:"json",data:l,success:function(o){if(o.error){var p=new Error(o.message);p.code=o.code;n(p)}else{n(null,o.data)}},error:function(q,o,p){n(p)}})},getRecord:function(l,n,m){path=f.record;path=path.replace("layer",l).replace("id",n);return this.request(path,{},m)},getRecords:function(l,m,n){path=f.records;idString=m.join(",");path=path.replace("layer",l).replace("ids",idString);return this.request(path,{},n)},getHistory:function(l,o,m,n){if(n===d){n=m;m={}}path=f.history;path=path.replace("layer",l).replace("id",o);return this.request(path,m,n)},getNearby:function(l,n,o,m,p){if(p===d){p=m;m={}}path=f.nearby;path=path.replace("layer",l).replace("arg",n+","+o);return this.request(path,m,p)},getNearbyGeohash:function(l,n,m,o){if(o===d){o=m;m={}}path=f.nearby;path=path.replace("layer",l).replace("arg",n);return this.request(path,m,o)},getNearbyAddress:function(l,m,n){path=f.nearbyAddress;path=path.replace("lat",l).replace("lon",m);return this.request(path,{},n)},getDensity:function(n,o,m,l,p){if(p===d){p=l;l=d;path=f.densityDay;path=path.replace("day",m).replace("lat",n).replace("lon",o)}else{path=f.densityHour;path=path.replace("day",m).replace("hour",l).replace("lat",n).replace("lon",o)}return this.request(path,{},p)},getContains:function(l,m,n){path=f.contains;path=path.replace("lat",l).replace("lon",m);return this.request(path,{},n)},getOverlaps:function(n,l,p,m,o,q){if(q===d){q=o;o={}}path=f.overlaps;path=path.replace("south",n).replace("west",l).replace("north",p).replace("east",m);return this.request(path,o,q)},getBoundary:function(m,l){path=f.boundary;path=path.replace("id",m);return this.request(path,{},l)},getLocation:function(l,n){var m;if(n===d){n=l;m=f.locateSelf}else{m=f.locate.replace("ip",l)}return this.request(m,{},n)}};return{Client:k}})(c);return a})(window);
</script>
<script type="text/javascript" src="http://github.com/simplegeo/polymaps/raw/v2.2.0/polymaps.js"></script>
</head>
<body>
<script type="text/javascript">
client = new simplegeo.Client("yXzFggJwLze5w82F9RtcsEf3Z6SZT2yZ");
client.getLocation(loadMap);
var po = org.polymaps;
function loadMap(err, data) {
if (err) {
console.error(err)
} else {
lat = data['geometry']['coordinates'][1];
lon = data['geometry']['coordinates'][0];
var map = po.map()
.container(document.body.appendChild(po.svg("svg")))
.add(po.interact())
.add(po.hash())
.center({lat: lat, lon: lon})
.zoom(13)
.zoomRange([12, 16]);
map.add(po.image()
.url(po.url("http://{S}tile.cloudmade.com"
+ "/1a1b06b230af4efdbb989ea99e9841af"
+ "/998/256/{Z}/{X}/{Y}.png")
.hosts(["a.", "b.", "c.", ""])));
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment