PatrickFrankAIU / GradeManagerProject

Grade Manager project for ITWEB 210 and ITWEB 220, 2402B Term
0 stars 0 forks source link

Right-Click Menu and Geolocation #9

Open PatrickFrankAIU opened 4 months ago

PatrickFrankAIU commented 4 months ago

To display the time zone:

HTML addition:

    <h1>Local Time Zone Information</h1>
    <div id="timezone-info"></div>

JavaScript addition:

document.addEventListener("DOMContentLoaded", () => {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {
            displayLocalTimeZone();
        });
    } else {
        document.getElementById('timezone-info').innerText = "Geolocation is not supported by this browser.";
    }
});

function displayLocalTimeZone() {
    const now = new Date();
    const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    const localTime = now.toLocaleTimeString();
    const gmtOffset = -now.getTimezoneOffset() / 60; // in hours

    const timezoneInfo = `
        <p>Time Zone: ${timeZone}</p>
        <p>GMT Offset: ${gmtOffset} hours</p>
        <p>Local Time: ${localTime}</p>
    `;
    document.getElementById('timezone-info').innerHTML = timezoneInfo;
}

Older notes:

Here's the code to trigger geolocation:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition((position) => {
    const { latitude, longitude } = position.coords;
    console.log(`Latitude: ${latitude}, Longitude: ${longitude}`);
  });
} else {
  console.log('Geolocation is not supported by this browser.');
}

Note: All this does is bring up this alert: image This will return the user's precise latitude and longitude, which is displayed in the Console window. At that point you're "in business", but you'll need to research how to use this data.

PatrickFrankAIU commented 4 months ago

Incidentally, the data from the console window can actually be pasted into Google Maps. You have to strip off the headers but leave the comma and the minus sign intact, like this:

"12.345678, -12.345678" <-- paste this into Google Maps' search window (without the quotation marks)

If you paste in your own data you can see how accurate it is. Kinda scary, eh?