This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How to access the correct `this` inside a callback
(13 answers)
unable to cope with the asynchronous nature of navigator.geolocation
(5 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I try to get my current browser location and set it in variables. If browser doesn't have activated geolocation set a default latitude and longitude.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(location) {
this.latitude = location.coords.latitude;
this.longitude = location.coords.longitude;
});
}else{
this.latitude = 43.318820;
this.longitude = -3.004561
}
console.log(this.latitude); //Undefinied
console.log(this.longitude);//Undefinied
The problem: Variables return undefinied in every cases. So, ¿What´s wrong in this code?
EDIT
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(location => {
this.latitude = location.coords.latitude;
this.longitude = location.coords.longitude;
});
}else{
this.latitude = 43.318820;
this.longitude = -3.004561
}
console.log(this.latitude);
console.log(this.longitude);
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed last month.
I would like to ask you to help me solve the following question which is giving me trouble: How do I isolate the latitude and longitude variables to display them outside the function
navigator.geolocation.getCurrentPosition(getLatLon);
function getLatLon(position) {
var latitude = "";
var longitude = "";
latitude = position.coords.latitude;
longitude = position.coords.longitude;
}
document.write("Latitude is "+latitude);
document.write("Longitude is "+longitude);
var latitude;
var longitude;
navigator.geolocation.getCurrentPosition(getLatLon);
function getLatLon(position) {
latitude = "";
longitude = "";
latitude = position.coords.latitude;
longitude = position.coords.longitude;
}
document.write("Latitude is "+latitude);
document.write("Longitude is "+longitude)
declare them outside of the function or at the top of your script file(global variables)
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
Why does changing the values of status and output inside the callback of a function I await for doesn't change their values outside?
var status = '';
var output = '';
if (orderTranscription != "TRANSCRIPTION_ERROR") {
await runPython38Script ("processing.py", orderTranscription, (output) => {
const orderInfo = JSON.parse(output);
// Both of these have a value defined here when I console.log, not empty string
status = orderInfo.status ? "VALID" : "PROCESSING_ERROR";
output = orderInfo.output;
});
}
// When I try to console.log outside, I get empty string.
console.log(status);
console.log(output);
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 2 years ago.
I have created a function that tries to get lat long based on the address and after that, I want those to print on console
How to get values of coords.... am I doing it in the correct way? please help to understand it.
coords = getLocationCoords("Ambala,Haryana,India,134003");
console.log(coords)
function getLocationCoords(fa){
var coords = [];
$.get(location.protocol + '//nominatim.openstreetmap.org/search?format=json&q='+fa, function(data){
coords['lat'] = JSON.parse(data[0].lat);
coords['lon'] = JSON.parse(data[0].lon);
});
return coords;
}//getLocationCoords
You cannot get the coords as "returned value", because there is nothing to return. That is, nothing to return until some handler is invoked, which could be 0.2 seconds later, or 3 seconds later. But if the handler returns it, you still won't be getting it since the assignment happened immediately. To solve this, you can also just make it a "promise", so that when the value is ready, you can then() it, and process the value, like follows:
getLocationCoords("Ambala,Haryana,India,134003")
.then(data => {
coords = data.map(loc =>
({
lat: Number(loc.lat),
lon: Number(loc.lon)
})
);
console.log("GOT IT", coords);
});
function getLocationCoords(fa) {
return $.get(location.protocol + '//nominatim.openstreetmap.org/search?format=json&q=' + fa)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 3 years ago.
I can not access the value of msgdata outside getJSON().
function editPost(event) {
event.preventDefault();
var msgdata = 'Hi';
$.getJSON('/users', function (data) {
msgdata = prompt("Enter value");
});
var newMessage = { 'message': msgdata }
}
Any suggestions ?
Because, the GET request completed after newMessage assignment.
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I am trying to read the enteredValue variable outside the function but the value outside the function remains undefined:
var enteredValue;
$("input").on("keydown",function search(e) {
if(e.keyCode == 13) {
alert($(this).val());
enteredValue = $(this).val().trim().toUpperCase()
console.log("Code inside: " + enteredValue); //value is read
}
});
console.log("Code outside : " + enteredValue); //value undefined
Can you please tell me what I'm doing wrong?