Pushing getCurrentPosition() values into array but cannot console log elements of the array
up vote
0
down vote
favorite
I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.
const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);
I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):
new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});
Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!
javascript geolocation
add a comment |
up vote
0
down vote
favorite
I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.
const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);
I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):
new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});
Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!
javascript geolocation
If you try to logarrLocations
from withingetLocation()
, it returns empty too...So you might want to start from there
– tera_789
Nov 11 at 6:03
I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.
const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);
I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):
new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});
Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!
javascript geolocation
I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.
const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);
I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):
new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});
Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!
javascript geolocation
javascript geolocation
asked Nov 11 at 5:59
Jacob L
33
33
If you try to logarrLocations
from withingetLocation()
, it returns empty too...So you might want to start from there
– tera_789
Nov 11 at 6:03
I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14
add a comment |
If you try to logarrLocations
from withingetLocation()
, it returns empty too...So you might want to start from there
– tera_789
Nov 11 at 6:03
I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14
If you try to log
arrLocations
from within getLocation()
, it returns empty too...So you might want to start from there– tera_789
Nov 11 at 6:03
If you try to log
arrLocations
from within getLocation()
, it returns empty too...So you might want to start from there– tera_789
Nov 11 at 6:03
I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14
I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
getCurrentPosition()
is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations
before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.
Here's one way. Just resolve
the array you want and take advantage of getCurrentPosition
's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):
const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}
// to use it:
getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))
That worked. Thanks!
– Jacob L
Nov 11 at 13:00
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
getCurrentPosition()
is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations
before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.
Here's one way. Just resolve
the array you want and take advantage of getCurrentPosition
's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):
const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}
// to use it:
getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))
That worked. Thanks!
– Jacob L
Nov 11 at 13:00
add a comment |
up vote
0
down vote
accepted
getCurrentPosition()
is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations
before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.
Here's one way. Just resolve
the array you want and take advantage of getCurrentPosition
's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):
const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}
// to use it:
getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))
That worked. Thanks!
– Jacob L
Nov 11 at 13:00
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
getCurrentPosition()
is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations
before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.
Here's one way. Just resolve
the array you want and take advantage of getCurrentPosition
's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):
const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}
// to use it:
getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))
getCurrentPosition()
is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations
before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.
Here's one way. Just resolve
the array you want and take advantage of getCurrentPosition
's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):
const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}
// to use it:
getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))
const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}
// to use it:
getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))
const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}
// to use it:
getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))
answered Nov 11 at 6:15
Mark Meyer
30.4k32550
30.4k32550
That worked. Thanks!
– Jacob L
Nov 11 at 13:00
add a comment |
That worked. Thanks!
– Jacob L
Nov 11 at 13:00
That worked. Thanks!
– Jacob L
Nov 11 at 13:00
That worked. Thanks!
– Jacob L
Nov 11 at 13:00
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246252%2fpushing-getcurrentposition-values-into-array-but-cannot-console-log-elements-o%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you try to log
arrLocations
from withingetLocation()
, it returns empty too...So you might want to start from there– tera_789
Nov 11 at 6:03
I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14