Reputation: 57
I want to tag camera photos with geolocation (coordinates are fine). Here's a piece of my code:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
return position.coords.latitude + " " + position.coords.longitude;
}
and inside the method takePhoto() I have:
...
context.drawImage(video, 0, 0, 640, 480);
var loc = getLocation();
context.strokeText(loc, 10, 50);
...
My problem is that I don't know how to pass a variable that holds the location coordinates, I've tried many things but without success. Anybody any ideas?
Thanks.
EDIT: I would like it to look something like this:
Upvotes: 0
Views: 780
Reputation: 1833
you need async await for resolve this issue
function takePhoto(position) {
document.getElementById("x").innerHTML = position.coords.latitude + " " + position.coords.longitude;
}
function getLocation() {
return new Promise((resolve,reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
return resolve(position);
}, function(err) {
return reject(err);
});
} else {
return reject("Geolocation is not supported by this browser.");
}
})
}
(async function() {
const coords = await getLocation();
takePhoto(coords);
})().then(() => {
console.log("done")
}).catch((err) => {
console.error(err);
})
<div id="x">empty</div>
Upvotes: 1