-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoshare.js
101 lines (85 loc) · 2.95 KB
/
geoshare.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
// register service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/serviceworker.js', { scope: '/' }).then(function(reg) {
if(reg.installing) {
console.log('Service worker installing');
} else if(reg.waiting) {
console.log('Service worker installed');
} else if(reg.active) {
console.log('Service worker active');
}
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
var geo = document.getElementById("geo");
var osm = document.getElementById("osm");
var adress = document.getElementById("adress");
var osmlink = document.getElementById("osmlink");
var osmreverse = document.getElementById("osmreverse");
var copybtn = document.getElementById("copybtn");
var msg = document.getElementById("msg");
var accuracy = "" ;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(createLinks);
} else {
msg.innerHTML = "Geolocation is not supported by this browser.";
}
return ;
}
function createLinks(position) {
geouri = "geo:" + position.coords.latitude + "," + position.coords.longitude;
osmurl = "http://www.openstreetmap.org/?mlat="+ position.coords.latitude + "&mlon=" + position.coords.longitude ;
reverseurl = 'https://nominatim.openstreetmap.org/reverse?format=json&lat=' + position.coords.latitude + '&lon=' + position.coords.longitude ;
accuracy = Math.round( position.coords.accuracy ) ;
geo.innerHTML = geouri ;
osm.innerHTML = osmlink.innerHTML = osmurl ;
osmlink.setAttribute('href', osmurl );
}
function copyGeo( copytext ) {
getLocation(); // if device has moved
copytext.select();
document.execCommand('copy');
msg.innerHTML = "<em>" + copytext.innerHTML + "</em> copied" ;
copytext.blur() ;
}
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
function getStreetZipCity(adressfield) {
msg.innerHTML = "getting adress" ;
adress.innerHTML = "" ; // blank to avoid old location
getLocation(); // if device has moved
getJSON( reverseurl ).then(function(data) {
var adresslist = data.address ;
adress.innerHTML =
//~ adresslist.building +
adresslist.road + '\n' +
//~ adresslist.suburb +
//~ adresslist.city_district +
adresslist.postcode + ' ' +
adresslist.city + '\n' +
//~ adresslist.state_district +
//~ adresslist.state +
adresslist.country + '\n' +
"± " + accuracy + "m" ;
copyGeo( adressfield );
}, function(status) {
msg.innerHTML = status ; // TODO
});
}