Reputation: 97
i have the following code:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(posi) {
var lat = posi.coords.latitude;
var lon = posi.coords.longitude;
console.log(lat);
}, showError);
} else {
$(".position").html("Geolocation is not supported by this browser");
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}
alert(lat);
$(".weather").html("<p>" + lat + "</p>");
the problem is that at the alert(lat) at te bottom it says that it is undefined and at the start at the if condition the console.log(lat) works perfectly fine! what is wrong at the bottom? i have tried making global variables lon, lat before if but the same thing happens.
Upvotes: 0
Views: 62
Reputation: 5094
For reference, here is the corrected code, based on the earlier comments:
function showError(error) {
[...] // No change.
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(posi) {
var lat = posi.coords.latitude;
var lon = posi.coords.longitude;
console.log(lat);
alert(lat);
$(".weather").html("<p>" + lat + "</p>");
}, showError);
} else {
$(".position").html("Geolocation is not supported by this browser");
}
Upvotes: 1