1

I don't seem to be able to reach the contents of the object created. Here is my code:

function showPosition() {
    var geopos = new Object();
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            geopos.lat= parseFloat(position.coords.latitude);
            geopos.long= parseFloat(position.coords.longitude);
            console.log(geopos['long']);// Displays the information fine
        });
    } else {
        geopos['error']= "Sorry, your browser does not support HTML5 geolocation.";
    }
    console.log(geopos); //Shows the object fine 
    console.log(geopos.lat); //undefined 
    return geopos;
}

As you can see by the comments i am getting undefined here. How can i access this information? Also tried with the other notation geopos['lat'] and works exactly the same. I've also tried with the answers from How can I display a JavaScript object? with the following outcome:

console.log(JSON.parse(JSON.stringify(geopos))); //empty object
console.log(JSON.stringify(geopos));//"{}"
console.log(Object.keys(geopos));//empty array
console.log(Object.values(geopos));//empty array

But if i output the entire object (with console.log(geopos)) i get the correct values in the console like this:

Object { } ​lat: 43.85249638386875 long: -9.65398821778 : Object { … }

How can i access the contents of this objects and why it isn't working?

Mbotet
  • 170
  • 2
  • 17

0 Answers0