0

I'd like to get the user_longitude and user_latitude values ​​outside of the function, but I don't really understand how to do that. Why? This is the first time I'm dealing with a function that is a bit strange for me, because I understand that the success function is an argument in navigator.geolocation.getCurrentPosition(success) but at the same time it has an additional pos argument, which is later used in crd, so my additional question is:

What is the pos argument?

function success(pos) {
            const crd = pos.coords;
          
            console.log('Your current position is:');
            console.log(`Latitude : ${crd.latitude}`);
            console.log(`Longitude: ${crd.longitude}`);
            console.log(`More or less ${crd.accuracy} meters.`);

            const user_latitude = crd.latitude

            const user_longitude = crd.longitude

            return user_longitude, user_latitude
          }

        
          
        navigator.geolocation.getCurrentPosition(success)
  • It's an object returned by `getCurrentPosition` when it's successful - you can write `console.log(pos)` in the success function to inspect it. – Andy Aug 12 '22 at 05:35
  • @Andy Okey, i am dumb. Thank you for the answer. So how can i get that object values outside that function? – fasfrtewqt2354r2edrq Aug 12 '22 at 05:39
  • You don’t. You get it inside the function, and that’s where you do with it whatever you have to do. – deceze Aug 12 '22 at 05:40
  • @fasfrtewqt2354r2edrq you need to put/move all logic that depends on `user_longitude` & `user_latitude` inside `success` function (callback). – bogdanoff Aug 12 '22 at 05:42
  • @bogdanoff And that's what i was asking for, cause i don't understand how to call that values and i am pretty sure i wrote it in my question. I asked for that logic, when i was saying "This is the first time I'm dealing with a function that is a bit strange for me, because I understand that the success function is an argument in navigator.geolocation.getCurrentPosition(success) but at the same time it has an additional pos argument, which is later used in crd". – fasfrtewqt2354r2edrq Aug 12 '22 at 05:47
  • @bogdanoff For now these functions success(pos) and navigator.geolocation.getCurrentPosition(success) look for me like function_1(argument_1) and function_2(function_1) and nothing more. I can't call just inside function cause i need to fetch it via POST request. – fasfrtewqt2354r2edrq Aug 12 '22 at 05:51
  • @fasfrtewqt2354r2edrq so you need to send location to server my making `fetch` request ?. then call `fetch(url, ....)....` inside `success` function. – bogdanoff Aug 12 '22 at 06:54
  • you can also convert `navigator.geolocation.getCurrentPosition` into Promise if you know how to use Promise & you are confident about it. – bogdanoff Aug 12 '22 at 06:56
  • Maybe start here: https://stackoverflow.com/q/47907070/476 – deceze Aug 12 '22 at 06:57

0 Answers0