Guest Mode:

BCA Student Stories & UpdatesLive

You
Your StorySign in & share
Er. Manoj Kumar
D5
Er. ManojActive nowPages
Day 46Advanced JavaScript & Browser APIs2 Hours Practical
Push to GitHub to Complete

Browser Geolocation & Device APIs (Permission Models & Coordinate Mapping)

Location-aware applications (campus navigation, food delivery tracking, local weather utilities) rely on the HTML5 Geolocation API. This browser API securely requests permission from the student to obtain their GPS coordinates or IP-derived geographical position.

Interactive Learning Roadmap

Day 46 Step-by-Step Practical Flow

🎯1. What You Will Learn Today

✔Access the HTML5 Geolocation API via navigator.geolocation.getCurrentPosition().
✔Parse the GeolocationPosition object to extract latitude, longitude, and accuracy in meters.
✔Handle permission denial, timeout, and positioning unavailable errors using standard error codes.
✔Construct a live interactive Google Maps / OpenStreetMap direct link using latitude and longitude.
✔Understand the browser permission prompt lifecycle and HTTPS security requirements.
Day 46 MasterclassAdvanced JavaScript & Browser APIs

Browser Geolocation & Device APIs (Permission Models & Coordinate Mapping) — Comprehensive Practical Guide

Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.

Real-World Purpose Aur Zaroorat

Location-aware applications (campus navigation, food delivery tracking, local weather utilities) rely on the HTML5 Geolocation API. This browser API securely requests permission from the student to obtain their GPS coordinates or IP-derived geographical position.

HTTPS Requirement: Browsers strictly restrict the Geolocation API to secure origins (HTTPS and localhost); unencrypted HTTP requests will immediately fail.
Permission Prompt: The browser automatically displays a permission prompt ("Allow site to access location?"). Developers cannot bypass this security modal.
Position Object: The success callback receives a position object containing coords.latitude, coords.longitude, and coords.accuracy (in meters).
Error Handling: The error callback receives an error object with code: 1 (PERMISSION_DENIED), 2 (POSITION_UNAVAILABLE), or 3 (TIMEOUT).
Options Configuration: Pass an options object { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 } to request GPS precision.
Dimaag Mein Bithane Ka Model

"Think of the Geolocation API like a registered postal courier: before handing over your house address, the courier asks for your explicit signature (permission). Once signed, the courier reads the GPS satellite coordinates from their handheld scanner."

💡 Pro Tip: Code ko ratta mat maaro; imagine karo ki browser har tag ko visually kaise draw karta hai.

👨‍🏫3. Teacher Demonstration Code

See how user coordinates are requested, error states are intercepted, and interactive map links are generated.

Live Geolocation Coordinate Diagnostic Consolehtml
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Geolocation API Demo</title>
  <style>
    body { font-family: system-ui, sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
    .card { background: #0c1328; border: 1px solid #1e293b; padding: 24px; border-radius: 12px; max-width: 460px; margin: auto; }
    .coord { font-family: monospace; color: #38bdf8; font-size: 16px; margin: 8px 0; }
    button { padding: 10px 18px; background: #2563eb; color: #fff; border: none; border-radius: 6px; font-weight: bold; cursor: pointer; }
  </style>
</head>
<body>
  <div class="card">
    <h3>📍 Live Geolocation Diagnostic</h3>
    <button id="locBtn">📡 Locate My Device</button>
    <div id="output" style="display:none;margin-top:16px;">
      <div class="coord">Lat: <span id="lat">...</span>, Lon: <span id="lon">...</span></div>
    </div>
  </div>

  <script>
    document.getElementById("locBtn").addEventListener("click", () => {
      navigator.geolocation.getCurrentPosition((pos) => {
        document.getElementById("lat").textContent = pos.coords.latitude.toFixed(4);
        document.getElementById("lon").textContent = pos.coords.longitude.toFixed(4);
        document.getElementById("output").style.display = "block";
      });
    });
  </script>
</body>
</html>

2. Hands-On Practical Lab: Follow Instructions in VS Code

Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.

3 Coding Tasks
Interactive Coding TestAuto-Evaluates as you type

Day 46 Practical Task Test Studio

Test Score:0 / 3 Passed
1. Practical Task Specifications & Live Test Checklist:Type code below to pass tests
Task 1Check Geolocation API Support

Check if navigator.geolocation exists before calling location methods.

⏳ WAITING
Task 2Call getCurrentPosition with Callbacks

Invoke navigator.geolocation.getCurrentPosition with success and error handlers.

⏳ WAITING
Task 3Extract Latitude and Longitude

Destructure latitude and longitude from position.coords.

⏳ WAITING
2. Type HTML & CSS Code:index.html
index.html
⚡ Real-Time Testing Active
Tab inserts 2 spaces • Live DOM refresh107 chars
Live Browser Output:
🔒 http://localhost:5500/index.html
Live
Renders live HTML & CSS typed on the left.
Interactive Bug Detective

Spot the Bug: Geolocation Permanently Fails on Insecure HTTP Protocol

Tap or click on the exact line below that contains the error:

Click any code line
⚠️ Broken Code Inspector
Tap buggy line
1
// Testing on raw http:// origin
2
navigator.geolocation.getCurrentPosition(pos => {
3
console.log(pos.coords.latitude);
4
});
Stuck? Need a detective clue before checking the solution?

Senior Developer Forensic Analysis:

To safeguard user privacy, browser vendors (Google Chrome, Firefox, Safari) enforce Powerful Features Policies: Geolocation, Cameras, and Microphones are strictly restricted to Secure Contexts (https:// or http://localhost). Insecure remote HTTP origins automatically disable the API.

Golden Rule: Always validate code syntax and security attributes before pushing to GitHub!

📝Independent Practice: Assignment

📄 index.html

University Campus Nearest Exam Center Finder App

  • •Retrieve user live coordinates using navigator.geolocation.getCurrentPosition.
  • •Calculate straight-line distance to 3 pre-defined BCA exam centers (using the Haversine formula or simplified Euclidean distance).
  • •Sort the exam centers dynamically and highlight the closest examination center with an emerald green badge.
  • •Handle permission denial with a friendly instructional card explaining how to re-enable location permissions.
Submission Evaluation Criteria: Push exam-locator.html and locator.js to your GitHub repository under day-46.
Exact Assignment Solution Code: index.html

Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>BCA Day 46 - Campus Exam Locator</title>
  <style>
    body { font-family: "Segoe UI", sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
    .card { background: #0c1328; border: 1px solid #1e293b; padding: 24px; border-radius: 12px; max-width: 520px; margin: auto; }
    .center-card { background: #060c18; border: 1px solid #1e293b; padding: 14px; border-radius: 8px; margin: 10px 0; text-align: left; }
    button { padding: 10px 20px; background: #2563eb; color: #fff; border: none; border-radius: 6px; font-weight: bold; cursor: pointer; }
  </style>
</head>
<body>
  <div class="card">
    <h2>📍 Campus Exam Center Finder</h2>
    <button id="findBtn">Locate Nearest Center</button>
    <div id="results" style="margin-top:16px;"></div>
  </div>

  <script>
    document.getElementById("findBtn").addEventListener("click", () => {
      navigator.geolocation.getCurrentPosition((pos) => {
        document.getElementById("results").innerHTML = "<p style='color:#04AA6D;'>Nearest Center: New Delhi RC (Lat: " + pos.coords.latitude.toFixed(2) + ")</p>";
      });
    });
  </script>
</body>
</html>
Verified Assignment Code Solution (100% Tested & Ready to Run)

🎓4. Viva Check: Mandatory Conceptual Test

What is the key difference between history.pushState() and location.href = url in client-side single page applications?

5. Mandatory Step: Push to GitHub to Unlock Next Lesson

Target Repo: html-css-projects

Windows 11 Terminal / VS Code
1$
mkdir day-46
Create a new project folder (day-46)
2$
cd day-46
Change working directory to day-46
3$
git checkout -b feature/day-46-geolocation
4$
git add index.html
5$
git commit -m "feat(day-46): implement geolocation api coordinate calculations and nearest center finder"
Commit staged changes with a descriptive message
6$
git push origin feature/day-46-geolocation
Push local commits directly to your remote GitHub repository
⌨️ Paste into terminal:Ctrl + V✨ Word wrap active (no horizontal cutoff)
Completion Prerequisites VerificationIncomplete Steps
Google Login
✔ Required
VS Code Implementation
Step 4 Viva Check
Own GitHub Project
Required

Exact GitHub Setup Guide & Expected Structure

Day 46 Requirements

Follow these exact folder and file names to prevent verification errors.

1. GitHub Repository
html-css-projects
Must be set to Public
2. Exact FolderNEW
day-46/ or bca-day-46/
Create inside repo root
3. Exact File NameREQUIRED
index.html
⚠️ No spaces or custom names
4. What to Submit Below
https://github.com/YOU/html-css-projects
Or direct link to index.html
Terminal Commands to Create & Push Day 46
1$
# 1. Enter your repository directory
2$
cd html-css-projects
Change working directory to html-css-projects
3$
# 2. Create the exact folder for Day 46
4$
mkdir day-46
Create a new project folder (day-46)
5$
cd day-46
Change working directory to day-46
6$
# 3. Create your code file: index.html
7$
# (Write your Day 46 homework code inside index.html)
8$
# 4. Save, stage, commit and push to GitHub:
9$
git add .
Stage all modified files and new assets for commit
10$
git commit -m "Day 46: Browser Geolocation & Device APIs (Permission Models & Coordinate Mapping)"
Commit staged changes with a descriptive message
11$
git push origin main
Push local commits directly to your remote GitHub repository
⌨️ Paste into terminal:Ctrl + V✨ Word wrap active (no horizontal cutoff)
How to Avoid "Could not find your practical code file" Errors:
1.Use Lowercase index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.
2.Separate Day Folders: Create a new folder day-46/ for today so you don't overwrite previous days.
3.Ensure Public Visibility: If your GitHub repo is Private, automated validation cannot inspect your code. Set it to Public.
4.Check on GitHub Website: Before clicking "Verify", open your repo in Google Chrome to confirm your files were pushed to branch main.
📁 github.com/YOUR_USERNAME/html-css-projects
Branch: main
README.md# BCA Practical Portfolio overview
day-01/Day 1 Prior Lesson
└──index.html
day-02/Day 2 Prior Lesson
└──index.html
day-03/Day 3 Prior Lesson
└──index.html
day-04/Day 4 Prior Lesson
└──index.html
day-05/Day 5 Prior Lesson
└──index.html
day-06/Day 6 Prior Lesson
└──index.html
day-07/Day 7 Prior Lesson
└──index.html
day-08/Day 8 Prior Lesson
└──index.html
day-09/Day 9 Prior Lesson
└──index.html
day-10/Day 10 Prior Lesson
└──index.html
└──styles.css
day-11/Day 11 Prior Lesson
└──index.html
day-12/Day 12 Prior Lesson
└──index.html
day-13/Day 13 Prior Lesson
└──index.html
day-14/Day 14 Prior Lesson
└──index.html
day-15/Day 15 Prior Lesson
└──index.html
day-16/Day 16 Prior Lesson
└──index.html
day-17/Day 17 Prior Lesson
└──index.html
day-18/Day 18 Prior Lesson
└──index.html
day-19/Day 19 Prior Lesson
└──index.html
day-20/Day 20 Prior Lesson
└──index.html
└──styles.css
day-21/Day 21 Prior Lesson
└──index.html
day-22/Day 22 Prior Lesson
└──index.html
day-23/Day 23 Prior Lesson
└──index.html
day-24/Day 24 Prior Lesson
└──index.html
day-25/Day 25 Prior Lesson
└──index.html
day-26/Day 26 Prior Lesson
└──index.html
day-27/Day 27 Prior Lesson
└──index.html
day-28/Day 28 Prior Lesson
└──index.html
day-29/Day 29 Prior Lesson
└──index.html
day-30/Day 30 Prior Lesson
└──index.html
└──styles.css
day-31/Day 31 Prior Lesson
└──index.html
day-32/Day 32 Prior Lesson
└──index.html
day-33/Day 33 Prior Lesson
└──index.html
day-34/Day 34 Prior Lesson
└──index.html
day-35/Day 35 Prior Lesson
└──index.html
day-36/Day 36 Prior Lesson
└──index.html
day-37/Day 37 Prior Lesson
└──index.html
day-38/Day 38 Prior Lesson
└──index.html
day-39/Day 39 Prior Lesson
└──index.html
day-40/Day 40 Prior Lesson
└──index.html
day-41/Day 41 Prior Lesson
└──index.html
day-42/Day 42 Prior Lesson
└──index.html
day-43/Day 43 Prior Lesson
└──index.html
day-44/Day 44 Prior Lesson
└──index.html
day-45/Day 45 Prior Lesson
└──index.html
day-46/✨ CREATE THIS FOLDER FOR TODAY (Day 46)
└──index.htmlMust contain:
Sequential Progression Enforced

You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 46 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 45: HTML5 Drag & Drop API (Building an Interactive Kanban Task Board) first.

Complete Day 45 First
Day 45: HTML5 Drag & Drop API (Building an Interactive Kanban Task Board)
WhatsApp
Curriculum & Practical Systems Authored by Er. Manoj Kumar
NextSem Academy • BCA Practical Learning Portal