-
Notifications
You must be signed in to change notification settings - Fork 27
/
yqlgeo.js
136 lines (127 loc) · 3.83 KB
/
yqlgeo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 pidgets.geoip'+
' 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 !== null){
callback(o.query.results);
} else {
callback({error:o.query});
}
};
return {
get:get,
retrieved:retrieved,
ipin:ipin
};
}();