-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (60 loc) · 2.05 KB
/
index.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
const form = document.getElementById('form');
const $ip = document.getElementById('ip')
const $location = document.getElementById('location')
const $timeZone = document.getElementById('timeZone')
const $isp = document.getElementById('isp')
const API_KEY = 'at_JJfbw5G260aOreQhUjl4i9dRCBKRn';
let lat;
let lng;
let map = L.map('map');
function _geolocation(lat, lng) {
if(lat == undefined && lng == undefined) {
return true
}
}
function initMap() {
var markerIcon = L.icon({
iconUrl: './public/images/icon-location.svg',
iconSize: [46, 56], // size of the icon
iconAnchor: [23, 55], // point of the icon which will correspond to marker's location
});
map.setView([lat, lng], 17);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: false,
}).addTo(map);
L.marker([lat, lng], { icon: markerIcon }).addTo(map);
}
async function getData(ip = null) {
// try {
const response = await fetch(
`https://geo.ipify.org/api/v1?apiKey=${API_KEY}&ipAddress=${ip}`
);
const data = await response.json();
if('as' in data) {
lat = data.location.lat
lng = data.location.lng
// Change INFO
$ip.textContent = data.ip
$location.textContent = `${data.location.city}, ${data.location.country} ${data.as.asn}`
$timeZone.textContent = `UTC ${data.location.timezone}`
$isp.textContent = `${data.isp}`
if(!_geolocation(lat, lng)) {
initMap()
}
}
}
form.addEventListener('submit', function (event) {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const ip = formData.get('ip');
const ipSplit = ip.split('.').map((element) => Number(element));
const maxLength = ipSplit.every((currentValue) => currentValue <= 255);
if (maxLength && ipSplit.length == 4) {
getData(ip);
}
});
if(_geolocation(lat, lng)) {
getData('').then(() => {
initMap()
})
}