0

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: enter image description here

2
  • what you expect, can show example? Commented Jan 25, 2019 at 9:04
  • I added an edit with picture Commented Jan 25, 2019 at 9:15

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but that doesn't help cause I want to pass this variable to the method takePhoto() and put the coordinates ON the photo...
i've change code and now you can pass variable to the method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.