-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
54 lines (43 loc) · 1.62 KB
/
map.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
function setMap()
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(async position => {
const lat = position.coords.latitude;
const long = position.coords.longitude;
const data=await getWeatherData(lat,long);
// Creating map options
var mapOptions = {
center: [lat,long],
zoom: 16
}
// Creating a map object
var map = new L.map('map', mapOptions);
// Creating a Layer object
var layer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
maxzoom: 10,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
// Creating a marker
var marker = new L.marker([lat,long]);
marker.bindPopup(data.name).openPopup();
// On clicking map
map.on('click',async function(e){
console.log("Lat: "+e.latlng.lat+" Long: "+e.latlng.lng);
})
// Adding layer to the map
map.addLayer(layer);
map.addLayer(marker);
}
)
}
}
setMap();
async function getWeatherData(lat,long)
{
const api = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&units=metric&appid=ddfaba4398b491fa4ef3e29a5e934c6e`;
let response = await fetch(api);
let data = await response.json();
console.log(data);
return data;
}