0

I want to use lat and lng from the getLocation() function in my marker but it wont work.

I added the alert because if that one works everything will.

I tried var lat = ...
I tried world.lat = ...

Something with return values

function getLocation() {
var onSuccess = function (position) {
    console.log('Latitude: ' + position.coords.latitude + '\n' +
        'Longitude: ' + position.coords.longitude + '\n' +
        'Altitude: ' + position.coords.altitude + '\n' +
        'Accuracy: ' + position.coords.accuracy + '\n' +
        'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
        'Heading: ' + position.coords.heading + '\n' +
        'Speed: ' + position.coords.speed + '\n' +
        'Timestamp: ' + position.timestamp + '\n');

    lat = position.coords.latitude;
    $('.locationLatitude').text(lat);
    lng = position.coords.longitude;
    $('.locationLongitude').text(lng);
    console.log(`latitude: ${lat} longitude: ${lng}`);
};

function onError(error) {
    console.log('code: ' + error.code + '\n' +
        'message: ' + error.message + '\n');
    alert(`code: ${error.code}
    message: ${error.message}
    Please turn on your GPS`);
}

navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
alert(lat, lng);
function meOnMap() {
    marker2 = L.marker([lat, lng]).addTo(map).bindPopup('Your Location').openPopup();
}

I expected that the lat and lng values returns their values from in the other function, but they don't.

6
  • Did you try to set the lat and lng as global variables? Commented Oct 30, 2019 at 0:33
  • My question is how do I get them to be global variables? Commented Oct 30, 2019 at 0:34
  • 1
    avoiding using global var as much as possible is better but it you can then just equate the global var to new value Commented Oct 30, 2019 at 0:38
  • I think the reason the lat and lng does not have a value is that the getLocation() hasn't been executed yet. Commented Oct 30, 2019 at 0:49
  • 1
    You order a pizza via their webpage. As soon as hit enter you try to eat the pizza. That is what you are doing. The pizza has not been made and delivered yet. Asynchronous 101 Commented Oct 30, 2019 at 0:51

2 Answers 2

1

It looks like you want lat and lng to be in scope when referenced in your meOnMap function. The comments are on the right track, you could declare them as global variables. To do that, just put var lat, lng; at the top of your file.

Or you could add some paramaters to your meOnMap function declaration and call it inside your success callback function, after it does all the processing stuff:

function meOnMap(lat, lng) {
    marker2 = L.marker([lat, lng]).addTo(map).bindPopup('Your Location').openPopup();
}
var onSuccess = function (position) {
    ...
    meOnMap(lat,lng);
};
Sign up to request clarification or add additional context in comments.

Comments

0

Try to call your getLocation function.

.... 
getLocation();
alert(lat, lng);

1 Comment

I still get: Uncaught ReferenceError: lat is not defined

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.