Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Signed-off-by: Glory Gupta  <prikarshglory@gmail.com>
  • Loading branch information
Glorycs29 authored Sep 23, 2023
1 parent 7864cfe commit 45235ba
Show file tree
Hide file tree
Showing 5 changed files with 766 additions and 0 deletions.
44 changes: 44 additions & 0 deletions GeoLocation-API/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

// Enable CORS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});


// Dummy shop data
const shops = [
{ name: 'Shop 1', latitude: 40.12345, longitude: -74.54321 },
{ name: 'Shop 2', latitude: 40.54321, longitude: -74.12345 }
];

app.post('/nearestShop', (req, res) => {
const { userLatitude, userLongitude } = req.body;

// Calculate nearest shop
let nearestShop = null;
let shortestDistance = Number.MAX_VALUE;

for (const shop of shops) {
const distance = calculateDistance(userLatitude, userLongitude, shop.latitude, shop.longitude);
if (distance < shortestDistance) {
shortestDistance = distance;
nearestShop = shop;
}
}

res.json({ nearestShop });
});

function calculateDistance(lat1, lon1, lat2, lon2) {
// any other function
return Math.abs(lat1 - lat2) + Math.abs(lon1 - lon2);
}

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
12 changes: 12 additions & 0 deletions GeoLocation-API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Geolocation App</title>
</head>
<body>
<button onclick="getLocation()">Get My Location</button>
<div id="demo"></div>

<script src="script.js"></script>
</body>
</html>
Loading

0 comments on commit 45235ba

Please sign in to comment.