0

how can i put the position result out of the function getcurrentposition

var my_position;
        navigator.geolocation.getCurrentPosition(position);{

        });

i want that "my_position" get the result of getCurrentPosition

Yahia Mgarrech
  • 682
  • 9
  • 14

1 Answers1

1

Try this:

var my_position;

function success(pos) {
  my_position = Object.create(pos);
  var 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.');
};

navigator.geolocation.getCurrentPosition(success);

Check out the documentation for more information on navigator.geolocation.getCurrentPosition().

Aiias
  • 4,683
  • 1
  • 18
  • 34
  • it dosen't work "Uncaught Error: TypeMismatchError: DOM Exception 17 (anonymous function)" – Yahia Mgarrech Jun 23 '13 at 23:05
  • @YahiaMgarrech - I have replaced the anonymous function with a regular defined function. Let me know if that works for you. – Aiias Jun 23 '13 at 23:08
  • "my_position" still undefined :( – Yahia Mgarrech Jun 23 '13 at 23:20
  • @YahiaMgarrech - `my_position` will be undefined until `success()` has run. You will need to wait until `success()` has run before trying to use `my_position`. – Aiias Jun 23 '13 at 23:27
  • its not problem of timing ,"my_position" is changed in the function "success" but out of it its the same "my_position"==undefined (out of success() ) / "my_position"==pos ( in sucess() ) – Yahia Mgarrech Jun 23 '13 at 23:38
  • @YahiaMgarrech - Ah I see now. I have updated my answer to use `Object.create()` as you will need a deep copy of the `pos` object. See [Most elegant way to clone a JavaScript object](http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object) for more information on deep-copying Javascript objects. – Aiias Jun 23 '13 at 23:42
  • thats will not resolve the problem , i'll save position in hidden input in the success(pos){}, after that "my_position" will get the value of that input , thanks for help :) – Yahia Mgarrech Jun 23 '13 at 23:49
  • @YahiaMgarrech - No problem. If you have found another solution to your problem, please post it as an answer to your question so others can use it, too. – Aiias Jun 23 '13 at 23:50
  • Users with less than 10 reputation can't answer their own question for 8 hours after asking. You can answer in 7 hours. Until then please use comments, or edit your question instead. – Yahia Mgarrech Jun 23 '13 at 23:55
  • @YahiaMgarrech - You can post your answer in your question and people will upvote the question. – Aiias Jun 23 '13 at 23:57