Compare commits

...
Author SHA1 Message Date
Joaquin e1626f6ae9 feature/completed getting geolocation data 2025-05-06 14:46:01 -07:00
Joaquin 83be006a83 feature/geolocation implemented 2025-05-06 13:38:00 -07:00
3 changed files with 35 additions and 0 deletions

No files matched your search

+10
View File
@@ -28,6 +28,7 @@ app.set('views',path.join(__dirname, 'src/views'));
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
app.use("/static", express.static("./src/public")); app.use("/static", express.static("./src/public"));
app.use("/images", express.static("./src/public/images")); app.use("/images", express.static("./src/public/images"));
app.use(express.json());
/*** Database ***/ /*** Database ***/
const { connectMongo, getCollection } = require("./src/database/connection"); const { connectMongo, getCollection } = require("./src/database/connection");
@@ -69,6 +70,15 @@ app.get('/aboutUs', (req, res) => {
return res.status(status.Ok); return res.status(status.Ok);
}); });
app.post('/api/location', async (req,res) => {
const { latitude, longitude } = req.body;
const response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&result_type=country&key=${process.env.geolocation_api}`);
const data = await response.json();
res.json(data);
});
// Initialize database and start app // Initialize database and start app
initDatabase().then(() => { initDatabase().then(() => {
+24
View File
@@ -0,0 +1,24 @@
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
}
}
function success(position) {
const { latitude, longitude } = position.coords;
fetch("api/location" , {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ latitude, longitude })
}).then(res => res.json())
.then(data => {
console.log(data.results[0].formatted_address);
})
}
function error(err) {
console.error("Geolocation error: ", err);
}
+1
View File
@@ -6,5 +6,6 @@
</div> </div>
</div> </div>
</footer> </footer>
<script src="/static/scripts/geolocation.js"></script>
</body> </body>
</html> </html>