-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_geo_redirect.html
92 lines (75 loc) · 2.89 KB
/
simple_geo_redirect.html
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
<html>
<head><title>Where the F are you at?</title>
<!-- <script src="modernizr-1.5.min.js"></script> -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>
<script type="text/javascript" src="geo.js" charser="utf-8"></script>
<body>
<div class="moo"></div>
Craigslist
<script>
function great_circle_distance (lat1, lng1, lat2, lng2) {
// gives distance between two pairs of coordinates, in KM
dlong = lng2-lng1;
dlat = lat2-lat1;
a = Math.pow((Math.sin(dlat / 2)), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow((Math.sin(dlong / 2)),2);
c = 2 * Math.asin(Math.min(1, Math.sqrt(a)));
dist = 3956 * c;
return dist;
}
function get_location() {
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true,options:5000});
}
else{
alert("Functionality not available");
}
}
function keys(obj) { // http://stackoverflow.com/questions/890807/iterate-over-a-javascript-associative-array-in-sorted-order
var keys = [];
for(var key in obj)
{
keys.push(key);
}
return keys;
}
function sortAssoc(aInput) {
var aTemp = [];
for (var sKey in aInput)
aTemp.push([sKey, aInput[sKey]]);
aTemp.sort(function () {return arguments[0][1] < arguments[1][1]}); //problems with javascript and floats? WTF
//console.log(aTemp.length);
var aOutput = [];
for (var nIndex = aTemp.length-1; nIndex >=0; nIndex--){
//console.log(aTemp[nIndex][1]);
aOutput[aTemp[nIndex][0]] = aTemp[nIndex][1];
}
//console.log(aOutput);
return aOutput;
}
function success_callback(p) {
console.log('lat='+p.coords.latitude+';lon='+p.coords.longitude);
console.log(p.coords);
var distance=[];
$.getJSON("http://localhost/simple_geo_redirect/areas.json", function(areas) {
for (var hostname in areas) {
coords = areas[hostname];
a = great_circle_distance(coords[0], coords[1], p.coords.latitude, p.coords.longitude);
//console.log(hostname + " " + coords + " " + a);
// create a distance hash structure which maps hostname with the distance to our user
distance[hostname]=a;
}
//console.log(keys(distance).sort().reverse());
for (i in sortAssoc(distance)) { console.log(i) };
}, function() { alert("nope");} );
//dummy example to show if the function is working
//console.log(great_circle_distance(36.532381, -6.301035, p.coords.latitude.toFixed(2), p.coords.longitude.toFixed(2)));
}
function error_callback(p)
{
alert('error='+p.message);
}
$(document).ready(get_location);
</script>
</body>
</html>