-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (54 loc) · 1.87 KB
/
app.js
File metadata and controls
69 lines (54 loc) · 1.87 KB
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
var ip_address = '';
var city = '';
var country_code = '';
function getIP() {
async function json(url) {
return await fetch(url).then(res => res.json());
}
let apiKey = 'bcd5b4ee60b2c1b1cda872ad275d3c2be5ec7201a5e12eff02495b21';
json(`https://api.ipdata.co?api-key=${apiKey}`).then(data => {
ip_address = data.ip;
city = data.city;
country_code = data.country_code;
var latitude = data.latitude;
var longitude = data.longitude;
console.log(latitude, longitude);
mapit(latitude , longitude);
render_user_info(data);
});
}
function mapit(latitude,longitude){
console.log("working on map");
//function to get IP of user and display it on a map using leaflet js.
document.getElementById('user-ip-btn').addEventListener('click' , getIP);
var mymap = L.map('mapid').setView([latitude , longitude], 15);
L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(mymap);
}
function render_user_info(data) {
var infoTable = document.getElementById('user-info');
infoTable.className == infoTable.id == "info-table";
infoTable.innerHTML =
`<tr>
<th>Ip</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Postal code</th>
</tr>
<tr>
<td>${data.ip}</td>
<td>${data.city}</td>
<td>${data.region}</td>
<td>${data.country_name}</td>
<td>${data.postal}</td>
</tr>
`
infoTable.style.transition = "0.2s ease";
}
document.getElementById('user-ip-btn').addEventListener('click' , getIP);
document.getElementById('input-custom-ip').addEventListener('click' , () => {
var input = document.getElementById('ip-input').value;
getIP(input);
});