Answer the question
In order to leave comments, you need to log in
Displays a modal window temperature in KELVINS, how can I convert to Celsius, can someone tell me?
I can't figure out how to convert the temperature to Kelvin, can someone tell me
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {lat: 49.00, lng: 32.00}
});
var modal = document.getElementById('modal'),
res,
markerCoord,
marker,
markersArray = [],
infowindow,
contentString,
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
google.maps.event.addListener(map, 'click', function (e) {
markerCoord = {lat: e.latLng.lat(), lng: e.latLng.lng()};
makeMarker();
// Get ajax call to get weather info
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
res = JSON.parse(xmlhttp.responseText);
openModal(res);
}
else if (xmlhttp.status == 400) {
console.log('There was an error 400');
}
else {
console.log('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", "http://api.openweathermap.org/data/2.5/weather?lat=" + markerCoord.lat + "&lon=" + markerCoord.lng + '&apiKey=05d1a9582076e6fecf5c7a3a363b8328', true);
xmlhttp.send();
});
// Make clear all old markers and add new one
function makeMarker() {
for (var i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
marker = new google.maps.Marker({
position: markerCoord,
map: map,
title: 'Weather here'
});
markersArray.push(marker);
}
function openModal(res) {
contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<div><b>Place: </b><span id="place"> ' + res.name + '</span> </div>'+
'<p><b>Weather:</b> <span id="weather">' + res.weather[0].main + '</span></p>'+
'<p><b>Temp:</b> <span id="temp">' + res.main.temp +'</span>° K</p>'+
'<p><b>Wind speed:</b> <span id="wind">' + res.wind.speed + '</span> ms</p>'+
'<p><b>Humidity:</b> <span id="humidity">' + res.main.humidity + '</span>%</p>'+
'</div>';
infowindow = new google.maps.InfoWindow({
content: contentString
});
</pre> infowindow.open(map, marker);
}
}
</script>
Answer the question
In order to leave comments, you need to log in
Subtract 273 from Kelvin to get Celsius.
function openModal(res) {
contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<div><b>Place: </b><span id="place"> ' + res.name + '</span> </div>'+
'<p><b>Weather:</b> <span id="weather">' + res.weather[0].main + '</span></p>'+
'<p><b>Temp:</b> <span id="temp">' + (res.main.temp - 273) +'</span>° K</p>'+
'<p><b>Wind speed:</b> <span id="wind">' + res.wind.speed + '</span> ms</p>'+
'<p><b>Humidity:</b> <span id="humidity">' + res.main.humidity + '</span>%</p>'+
'</div>';
infowindow = new google.maps.InfoWindow({
content: contentString
});
infowindow.open(map, marker);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question