This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
99 lines (86 loc) · 3.04 KB
/
app.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
'use strict'
import API_KEY from './config.js'
const ipAddressField = document.querySelector('.ipAddressField')
const timezoneInput = document.querySelector('.timezoneInput')
const countryLocationInput = document.querySelector('.locationInput')
const ispInput = document.querySelector('.ispInput')
const submitBtn = document.querySelector('.submit-btn')
const inputField = document.querySelector('.input-field')
//Map
let map = L.map('map').setView([51.505, -0.09], 13)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map)
//API
let ipAddress
let randomIP = ''
let timeZone
let countryLocation
let cityLocation
let postalCode
let isp
let lat
let lng
let url = `https://api.ipgeolocation.io/ipgeo?apiKey=${API_KEY}=`
fetch(url)
.then((response) => response.json())
.then((response) => {
ipAddress = response.ip
timeZone = response.time_zone.offset
countryLocation = response.country_name
cityLocation = response.city
postalCode = response.zipcode
isp = response.isp
lat = response.latitude
lng = response.longitude
ipAddressField.innerHTML = ipAddress
timezoneInput.innerHTML = ` UTC ${timeZone}`
countryLocationInput.innerHTML = `${countryLocation}, ${cityLocation} ${postalCode}`
ispInput.innerHTML = isp
mapLocation(lat, lng)
}).catch(error => console.log(error))
const mapLocation = (lat, lng) => {
var markerIcon = L.icon({
iconUrl: '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)
}
//Search by IP + validation
submitBtn.addEventListener('click', (event) => {
event.preventDefault()
if (
inputField.value.match(
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
)
) {
randomIP = inputField.value
url = `https://api.ipgeolocation.io/ipgeo?apiKey=${API_KEY}=` + randomIP
fetch(url)
.then((response) => response.json())
.then((response) => {
ipAddress = response.ip
timeZone = response.time_zone.offset
countryLocation = response.country_name
cityLocation = response.city
postalCode = response.zipcode
isp = response.isp
lat = response.latitude
lng = response.longitude
ipAddressField.innerHTML = ipAddress
timezoneInput.innerHTML = ` UTC ${timeZone}`
countryLocationInput.innerHTML = `${countryLocation}, ${cityLocation} ${postalCode}`
ispInput.innerHTML = isp
mapLocation(lat, lng)
}).catch(error => console.log(error))
} else {
alert('You have entered an invalid IP address!')
return false
}
})