I want to use lat and lng from the getLocation() function in my marker but it wont work.
I added the alert because if that one works everything will.
I tried var lat = ...
I tried world.lat = ...
Something with return values
function getLocation() {
var onSuccess = function (position) {
console.log('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
lat = position.coords.latitude;
$('.locationLatitude').text(lat);
lng = position.coords.longitude;
$('.locationLongitude').text(lng);
console.log(`latitude: ${lat} longitude: ${lng}`);
};
function onError(error) {
console.log('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
alert(`code: ${error.code}
message: ${error.message}
Please turn on your GPS`);
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
alert(lat, lng);
function meOnMap() {
marker2 = L.marker([lat, lng]).addTo(map).bindPopup('Your Location').openPopup();
}
I expected that the lat and lng values returns their values from in the other function, but they don't.
latandlngdoes not have a value is that thegetLocation()hasn't been executed yet.