I'm building a simple application using vanilla JS in which I retrieve the user's location and pass down the coordinates to Google's Geolocation API. I'm trying to gain access to the API key by setting it as an environment variable through Netlify's UI and I don't quite understand exactly how to implement lambda functions to accomplish the task.
I have a function that gets the user's latitude/longitude and fetches the data from the geolocation API before displaying it in the DOM. As of now I only have an index.html and app.js file.
getUserLocation();
function getUserLocation() {
async function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const geoAPI = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=${apiKey}`;
const { city, state, country } = await getGeoData(geoAPI);
updateWidget({ city, state, country });
}
function error() {
alert("Unable to retrieve your location");
}
if (!navigator.geolocation) {
console.log("Geolocation is not supported by your browser");
} else {
navigator.geolocation.getCurrentPosition(success, error);
}
}
I tried reading Netlify's documentation but I'm not sure how to implement the solution to my simple project. Any help is much appreciated!
