-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
75 lines (63 loc) · 3 KB
/
test.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
<!DOCTYPE html>
<html>
<head>
<title>Embedded Map</title>
<!-- Include Leaflet CSS and JS -->
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
</head>
<body>
<!-- Create a container for the map -->
<div id="map" style="height: 80vh;"></div>
<script>
// Initialize the map
var mymap = L.map('map').setView([0, 0], 13);
// Add OpenStreetMap as the base layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(mymap);
// Get the user's current location and update the map
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
async function (position) {
// Update the map view to the user's location
mymap.setView([position.coords.latitude, position.coords.longitude], 13);
// Add a marker to the map at the user's location
L.marker([position.coords.latitude, position.coords.longitude], {
icon: greenMarkerIcon()
}).addTo(mymap).bindPopup('My Location').openPopup();
console.log("lat ", position.coords.latitude);
try {
var location = localStorage.getItem("mapType");
// Fetch data from the API using async/await
const response = await fetch(`https://bhuvan-app1.nrsc.gov.in/api/api_proximity/curl_hos_pos_prox.php?theme=${location}&lat=${position.coords.latitude}&lon=${position.coords.longitude}&buffer=3000&token=e9549b109e17ef4d45724c134ebeb1ff2e7feccf`);
const data = await response.json();
if (data) {
data.forEach(item => {
console.log(item);
L.marker([item.lat, item.lon]).addTo(mymap).bindPopup('Marker from API').openPopup();
});
}
} catch (error) {
console.error('Error fetching API data:', error.message);
}
},
function (error) {
console.error('Error getting user location:', error.message);
}
);
} else {
console.error('Geolocation is not supported by this browser.');
}
function greenMarkerIcon() {
return L.icon({
iconUrl: 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-green.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
}
</script>
</body>
</html>