all in code i'm trying give user location , temperature in locality somehow temperature showing way less in celsius , not updating below tried 4-5 hours data 10 degree celsius less if temp 22(celsius) hours showing 3(celsius) working example on codepen http://codepen.io/cannelflow/full/rrymyo/
var x = document.getelementbyid("demo"); var y = document.getelementbyid("demo1"); window.onload = getlocation(); //window.onload=getweather(); function getlocation() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(showposition, showerror); } else { x.innerhtml = "geolocation not supported browser."; } } //location display function showposition(position) { var loc = { lat: position.coords.latitude, lon: position.coords.longitude }; getweather(loc); var baseurl = "https://maps.googleapis.com/maps/api/geocode/json?latlng="; var fullurl = baseurl + loc.lat + "," + loc.lon; $.ajax({ url: fullurl, success: function (display) { x.innerhtml = display.results[1].formatted_address; } }); } //location weather function getweather(loc) { var baseurl = "http://api.openweathermap.org/data/2.5/weather?lat="; var appid = "064129b86c99c35c42d531db251b99e3"; var fullurl = baseurl + loc.lat + "&lon=" + loc.lat + "&appid=" + appid + "&units=metric"; //http://api.openweathermap.org/data/2.5/weather?lat=21.2600668&lon=81.5989561&appid=064129b86c99c35c42d531db251b99e3&units=metric $.ajax({ url: fullurl, success: function (display1) { y.innerhtml = display1.main.temp; } }); } function showerror(error) { switch (error.code) { case error.permission_denied: x.innerhtml = "user denied request geolocation." break; case error.position_unavailable: x.innerhtml = "location information unavailable." break; case error.timeout: x.innerhtml = "the request user location timed out." break; case error.unknown_error: x.innerhtml = "an unknown error occurred." break; } }
<body> <section> <div class="container-fluid text-center"> <br /> <!-- <h1><button class="btn btn-danger" onclick="getlocation()">click me location!</button></h1> --> <h1 class="text-primary" id="demo1"></h1> <br /> <h1 class="text-primary" id="demo"></h1> </div> </section> </body>
you have typo in query string. better alternative user jquery.param
build query string object instead easier read , less error prone.
function getweather(loc) { var baseurl = "http://api.openweathermap.org/data/2.5/weather?"; return $.ajax({ url: baseurl + $.param({ appid: "064129b86c99c35c42d531db251b99e3", lon: loc.lon, lat: loc.lat, units: "metric" }), success: function(display1) { y.innerhtml = display1.main.temp; } }); }
Comments
Post a Comment