0% found this document useful (0 votes)
3 views

Lab20 20Geolocation Functionality.docx 1739518892067

The document outlines a lab experiment focused on implementing geolocation functionality using simulated data. Students will create a function to simulate user location and modify an existing weather forecast function to incorporate this geolocation data. The lab includes tasks for simulating location, generating weather forecasts, and outputting weather data for the detected location over three days.

Uploaded by

latika.shelar69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab20 20Geolocation Functionality.docx 1739518892067

The document outlines a lab experiment focused on implementing geolocation functionality using simulated data. Students will create a function to simulate user location and modify an existing weather forecast function to incorporate this geolocation data. The lab includes tasks for simulating location, generating weather forecasts, and outputting weather data for the detected location over three days.

Uploaded by

latika.shelar69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment 9
Implementing Geolocation
Functionality
Exp. 9 Implementing Geolocation Functionality

Lab 9 Objectives:
●​ Implement geolocation functionality to detect the user's current location (using
simulated data)

Prerequisites: The prerequisites of this lab are-


●​ Completion of previous labs
●​ Basic understanding of JavaScript functions, arrays, objects, and the Geolocation AP

Outcomes: By the end of this lab, students will be able to:
●​ Use simulated geolocation data to detect the user's current location.
●​ Integrate the geolocation data into the existing weather forecast function.

Description:

1.​ Task 1: Simulate Geolocation Data


Create a new function getUserLocation() that simulates the detection of the user's
current location (latitude and longitude).
Return a fixed set of coordinates for a specific location (e.g., New York: latitude =
40.7128, longitude = -74.0060).

Example
function getUserLocation() {
// Simulated location data for New York
return {
latitude: 40.7128,
longitude: -74.0060
};
}

2.​ Task 2: Modify the Weather Forecast Function


Modify the generateWeatherForecast() function to accept latitude and longitude as
arguments, in addition to the city.
Use the geolocation data from getUserLocation() to determine the location for
generating weather data.

Example
function generateWeatherForecast(city, latitude, longitude) {
const weatherConditions = ["Sunny", "Cloudy", "Rainy", "Snowy"];
const forecast = [];
const currentDate = new Date();

for (let i = 0; i < 3; i++) {


const day = currentDate.getDate();
const month = currentDate.getMonth() + 1;
const year = currentDate.getFullYear();

const temperature = Math.random() * 45 - 10;


const condition = weatherConditions[Math.floor(Math.random() *
weatherConditions.length)];
const humidity = Math.random() * 100;

forecast.push({
date: `${month}/${day}/${year}`,
temperature,
condition,
humidity,
latitude,
longitude
});

currentDate.setDate(currentDate.getDate() + 1);
}
return forecast;
}

3.​ Task 3: Call the Function with Simulated Location


Call getUserLocation() to get the simulated latitude and longitude.
Pass these values to generateWeatherForecast().
Example:
const city = "New York";
const location = getUserLocation();
const forecastData = generateWeatherForecast(city, location.latitude,
location.longitude);
console.log(forecastData);

4.​ Task 4: Output Weather Data with Location


Ensure the output contains weather data for the user's detected location, including
latitude, longitude, temperature, condition, and humidity for the next 3 days.

You might also like