I am using the Google Geocoder API to get coordinates given an address, and I currently have this function:
var latitude = 0;
var longitude = 0;
const test = getGeocode(
location[0].address,
process.env.NEXT_PUBLIC_API_KEY,
).then(
(response) => {
latitude = response.data.results[0].geometry.location.lat;
longitude = longitude = response.data.results[0].geometry.location.lng;
},
(error) => {
console.error(error);
},
);
When I console.log the lat and long values inside of the const test, the values print correctly. However, I am unable to save these values into the longitude and latitude global variables as I need to. Is there anything I can do to save the long and lat values from my const test into global variables so that I can use them elsewhere? (My ultimate goal is to display the location of the long/lat on a map on my site.)