javascript - Temperature conversion with openweathermap API -
i'm building weather app openweathermap api. app work have problem temperature converter still doesn't work.
here html:
<div class='valign col s6 text-style'> <div id="icontemp"> <div id="icon"></div> <div id="temp"></div> </div> <div class='divider'></div><br> <div class="text-style-details"> <div class="location"></div> <div id="conditions"></div> <div id="wind"></div> </div> </div>
and javascript:
<script type="text/javascript"> function toc() { var strin = parseint(temperature); if(isnan(strin)) { alert("not number"); } else { var f = parsefloat(strin); var c = (f - 32) * 5/9; var r = math.round(c * 100)/100; parseint(temperature) = r.tostring(); } } function tof() { var strin = parsefloat((temperature).tofixed(1)); if(isnan(strin)) { alert("not number"); } else { var c = parsefloat(strin); var f = (c * 9/5) + 32; var r = math.round(f * 100)/100; parsefloat((temperature).tofixed(1)) = r.tostring(); } } $(document).ready(function() { getlocationdata(); function getlocationdata() { $.get("http://ipinfo.io", function(location) { console.log(location); $('.location') .append(location.city + ", ") .append(location.region); var units = getunits(location.country); getweatherdata(location.loc, units); }, "jsonp"); } function getweatherdata(loc, units) { lat = loc.split(",")[0] lon = loc.split(",")[1] var appid = "&appid=123456abcde" var url = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + "&units=" + units + appid; console.log(url); $.get(url, function(weather) { var winddir = winddirection(weather.wind.deg); var temperature = weather.main.temp; var unitlabel; if (units === "imperial") { unitlabel = "<a href='#'>f</a>/<a href='#' onclick='toc()'>c</a>"; // temperature = parsefloat((temperature).tofixed(1)); } else { unitlabel = "<a href='#' onclick='tof()'>c</a>"; // temperature = parseint(temperature); } // temperature; // temperature = parseint(temperature); // temperature = parsefloat((temperature).tofixed(1)); console.log(weather); $('#icon') // .append("<img src='http://openweathermap.org/img/w/" + weather.weather[0].icon + ".png'>"); .append("<i class='wi wi-owm-"+weather.weather[0].id+"'></i>"); // <i class="wi wi-owm-200"></i> $('#temp').append(temperature + "° " + unitlabel); $('#conditions').append(weather.weather[0].description); $('#wind').append(winddir + " " + weather.wind.speed + " knots"); }, "jsonp"); }; function winddirection(dir) { var rose = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']; var eightpoint = math.floor(dir / 45); return rose[eightpoint]; } function getunits(country) { var imperialcountries = ['us', 'id', 'sg', 'my', 'bs', 'bz', 'ky', 'pw']; if (imperialcountries.indexof(country) === -1) { var units = 'metric'; } else { units = 'imperial'; } console.log(country, units); return units; } }); </script>
the app pict:
i created function toc (convert f c) , tof ( convert c f), when click label error message "uncaught referenceerror: toc not defined". want app temperature value change when click c (celcius) , f (fahrenheit).
any appreciated greatly, thank you.
although writing kind of functions training good, answering main question according openweathermap api unit conversion support:
you can pass: &units=metric
api call in order temperature in celsius, this:
api.openweathermap.org/data/2.5/weather?lat=latitude&lon=longitude&units=metric&appid=your_api_key
here info official documentation:
units format description: standard, metric, , imperial units available.
parameters:
units metric, imperial. when not use units parameter, format standard default.
temperature available in fahrenheit, celsius , kelvin units.
for temperature in celsius use units=metric temperature in fahrenheit use units=imperial
examples of api calls:
standard api.openweathermap.org/data/2.5/find?q=london
metric
api.openweathermap.org/data/2.5/find?q=london&units=metric
imperial
api.openweathermap.org/data/2.5/find?q=london&units=imperial
Comments
Post a Comment