Weather Javascript and Jquery

Having trouble with getting Jquery to work with Javascript. the var lat and long do not get defined after calling the function. I need lat and long to be defined globally rather than only defined within showPosition().

So I can use it in an API call for the weather app. Below.

geoUrl= 'https://crossorigin.me/https://www.metaweather.com//api/location/search/?lattlong=’+ lat + ‘,’ + long;

var lat;
var long;
$(document).ready(function(){
setLocation();
console.log(lat);

});

function setLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert(“Geolocation is not supported by this browser.”);
}
}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
}

var lat;
var long;
$(document).ready(function(){
setLocation();

function setLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert(“Geolocation is not supported by this browser.”);
}
}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
console.log(lat);
}
});

The latitude is only defined within showPosition :frowning:. I need lat and long to be defined globally.

to show this info at you page? well im not sure abt your lvl , but when i was thinking abt the same thing a guy showed me an optional way i didnt think about , you can get this data anywhere you want from within that show position function , instead of console.log say smth like $(’#someDiv’).html('lat: '+lat);

Ah no so I can use it in an API call for a weather app. Below.

geoUrl= 'https://crossorigin.me/https://www.metaweather.com//api/location/search/?lattlong=’+ lat + ‘,’ + long;

you can place the whole API call within that function