Where is position.coords.latitude in the window object?

Viewed 633

Given these functions for determining user location:

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } 
    else {
        alert("Geolocation is not supported by this browser.");
    }
}

function showPosition(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
}

I have a high level understanding of how it is working, but I cannot for the life of me find where position.coords.latitude is coming from in the window object.

It makes sense for it to be in window.position.coords.latitude.... OR in somewhere in window.navigator.geolocation...

But it doesn't appear in either of those locations when I console.log the window.

Where is this located in the window object?

2 Answers
navigator.geolocation.getCurrentPosition()

is an async function. if you see navigator.geolocation.getCurrentPosition((a)=> console.log(a.timestamp)) callback response includes a timestamp also.

because this API fetches the co-ordinates every time it is being called, it doesn't store or caches the lat/long

hope this answers your quesiton.

for more info refer

If you do navigator.geolocation.getCurrentPosition(data => console.log(data)) in your console an alert will appear asking you to confirm that you want to share your location. Press allow. You will then in the console that getCurrentPosition() returns an object that contains a coords property. coords is also an object that has latitude and longitude (and a few other properties).

Related