I am trying to make a weather showing app using javascript from FCC project. In order to display the current weather first I have to find the location of the device. this works fine using HTML geolocation.But The latitude and longitude is giving me undefined
error when i try to use it outside the function eventhough I am trying to assign it to a global variable.
$(document).ready(function(){
var lon;
var lat;
function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
console.log("your device is not suported!");
}}
function showPosition(position){
var pos = position.coords
lat = pos.latitude;
lon = pos.longitude;
}
console.log(lat); // for debugging, here is the undefined error
$.ajax({
type: 'GET',
url: 'https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139',
success: function(data){
$('h1').html( "<strong>" + data.coord.lat + "</strong>" );
console.log(latitude);
},
error: function(error){
console.log(error);
}
});
});
Thank you for the help :)