I'm trying to search for nearby locations by first getting the last coordinates with geocode() then inserting it to nearbySearch() to find locations near it. On the console I'm meat with an Axios error:
//Error Access to XMLHttpRequest at 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=29.7162525,-95.4169039&radius=500&key=AjzaSyD7LSZB-~~~~~~~~~~~~~~~~~~~~~~~' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
scripts.js:88 q {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK', config: {…}, request: XMLHttpRequest, …}
xhr.js:247 GET https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=29.7162525,-95.4169039&radius=500&key=AjzaSyD7LSZB-~~~~~~~~~~~~~~~~~~~~~~~ net::ERR_FAILED 200
However, when I place the https to Postman, there is no error and everything is working as expected. What are some potential issue?
//JS code
let lastCoordinates
function geocode(){
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params:{
address: lastAddress,
key:'AjzaSyD7LSZB-~~~~~~~~~~~~~~~~~~~~~~~'
}
})
.then(function(response){
console.log(response);
endAddress = response.data.results[0].geometry.location;
lastCoordinates = endAddress.lat + "," + endAddress.lng;
})
.catch(function(error){
console.log(error);
})
}
nearbyBtn = document.getElementById('nearbyBtn');
nearbyBtn.addEventListener('click', nearbySearch);
// Not working //
function nearbySearch(){
axios.get('https://maps.googleapis.com/maps/api/place/nearbysearch/json', {
params:{
location: lastCoordinates,
radius: 500,
key:'AjzaSyD7LSZB-~~~~~~~~~~~~~~~~~~~~~~~'
}
})
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
})
}
I tried placing a coordinate on location instead of a lastCoordinates. Maybe the issue was with the variable format. Same thing.
I tried putting the https on Postman to see if the error was with the url/credentials. My key was correct and the information was as expected.