0
Guys, I’m trying to get a user’s location and it keeps updating from time to time, searching, I found this method, however, I can not only do this with the current value of the array, it concatenates (push) and adds infinitely.. someone knows how I can solve this?
var array = [];
navigator.geolocation.watchPosition(function(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var vel = position.coords.speed;
array.push(lat, lon, vel);
locationCode()
});
function locationCode() {
console.log(array)
alert(array[0]);
}
And if you put
var array = [];
at the beginning of the function?– Sam
What do you mean? I don’t understand
– Matheus Lorran Silva
You can create an object and update it instead of an array, in my opinion it is even clearer
– Costamilam
That line
var array = [];
is out of functionfunction(position) {
, right? If you put in, right at the beginning?– Sam
So every time the function is called, the array will be restarted from scratch.
– Sam
I’ll try, I’ll put her right at the beginning, thanks for the idea, I’ll test.
– Matheus Lorran Silva
unfortunately did not, array is not defined
– Matheus Lorran Silva
Keep the array out and within zero it
var array = []; navigator.geo... { array = []
– Costamilam
Right. Then leave it as it was, and within the function puts only
array = [];
, without thevar
– Sam
Dude, you guys are geniuses, it worked!
– Matheus Lorran Silva
There @Guilhermecostamilam, put the answer there.
– Sam
Guys, now another problem has arisen as always in informatica, how can I get the value of the array out of the function? I need it to mark the user’s real-time location on the map
– Matheus Lorran Silva