-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivelocation.html
176 lines (80 loc) · 3.58 KB
/
livelocation.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get The Current Geo Location Of the user for free without any authentication (Accurate Location)</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<style>
#address {
display: none;
padding: 20px;
background-color: #f8f9fa;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.loader {
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
width: 20px;
height: 20px;
animation: spin 1s linear infinite;
margin-right: 5px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div id="content">
<div id="loader" class="flex items-center justify-center mt-4">
<div class="loader"></div>
<span>Loading...</span>
</div>
<div id="address" class="hidden">
<h2 class="text-2xl font-semibold mb-2">Address Information</h2>
<p class="mb-2"><strong>Full Address:</strong></p>
<p id="fullAddress"></p>
<p class="mb-2"><strong>City:</strong> <span id="city"></span></p>
<p class="mb-2"><strong>Country:</strong> <span id="country"></span></p>
<!-- Add more styling for additional address details if needed -->
</div>
</div>
<script>
// Chech The Current Accurate Geo Location Of the Client
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// Make the API request inside the geolocation callback
$.get("https://nominatim.openstreetmap.org/search?q=" + latitude + "," + longitude + "&format=json&polygon=1&addressdetails=1", function(response) {
// Hide loader when content is loaded
$('#loader').hide();
if (response.length > 0) {
var fulladdress = response[0].address;
var city = response[0].address.city;
var country = response[0].address.country;
// Show content when available
$('#address').show();
// Append address content
$('#fullAddress').text(fulladdress);
$('#city').text(city);
$('#country').text(country);
// Object.entries(fulladdress).forEach(([key, value]) => {
// $("#address").append(key.charAt(0).toUpperCase() + key.slice(1) + " : " + value + "<br>");
// });
} else {
console.log("No address data found.");
}
});
});
} else {
console.log("Geolocation is not supported by this browser.");
}
</script>
</body>
</html>