Hello,
This is my javascript file, and my index page just has some paragraphs with ids. I have the data, and it’s populating. My question is how can I refactor this, is it best practice to do everything in the success property like this? I figured since I have the basic of what needs to be done there, maybe I can refactor. Looking for insight.
$(document).ready(function () {
var init = {
api: 'http://api.openweathermap.org/data/2.5/weather?',
apiKey: ''
};
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var yourLatitude = position.coords.latitude;
var yourlongitude = position.coords.longitude;
$.ajax({
url: init.api + 'lat=' + yourLatitude + '&lon=' + yourlongitude + '&appid=' + init.apiKey,
dataType: 'jsonp',
success: function(results){
var city = results.name;
var overcast = results.weather[0].main;
var tempature = convertToC;
$('#city').text(city);
$('#overcast').text(overcast);
$('#tempature').text(tempature);
function convertToC() {
return results.main.temp - 273.15;
}
console.log(results);
}
});
});
} else {
console.log('Failed');
}
});