summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/www/js/lib/etc/geo.js
blob: 885a1c6ce7b1b3ab3dc5d1197f9899677d61df3b (plain)
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
var geo = (function(){
  var geo = {}
  
  var polling = false, fetching = false, poll_timeout = null
  
  geo.fetch = function(){
    fetching = true
    navigator.geolocation.getCurrentPosition(geo.success, geo.error, {timeout: 15000})
  }
  
  geo.success = function(position){
    var lat_str = as_degrees( position.coords.latitude || 40.99167, "N", "S" )
    var lng_str = as_degrees( position.coords.longitude || -74.07944, "E", "W" )
    $(".latlng").html( lat_str + "     " + lng_str )
    geo.done()
  }
  
  geo.error = function(error){
    $(".latlng").html( "+40° 58' 90.9\" N     74° 04' 46.3\" W" )
    geo.done()
  }
  
  geo.done = function(){
    fetching = false
    if (polling) {
      clearTimeout( poll_timeout )
      poll_timeout = setTimeout(geo.fetch, 15000)
    }
  }
  
  geo.start_polling = function(){
    polling = true
    if (! fetching) {
      geo.fetch()
    }
  }
  
  geo.stop_polling = function(){
    polling = false
    clearTimeout(poll_timeout)
  }
  
  function as_degrees (n, pos, neg) {
    var s = ""
    var sig = n >= 0 ? pos : neg
    
    s += Math.floor(n) + "° "
    
    n = Math.abs(n)
    n %= 1
    n *= 60
    nn = Math.floor(n)
    if (nn < 10) nn = "0" + nn
    s += nn + "' "
    
    n %= 1
    n *= 60
    nn = Math.floor(n)
    if (nn < 10) nn = "0" + nn
    s += nn
    
    n %= 1
    n *= 10
    nn = Math.floor(n)
    s += "." + nn + '\" ' + sig
    
    return s
  }

  return geo
})()