CORS Error when calling Google Maps Distance Matrix API

  • Goal: I'm trying to get the driving distance between two locations based on the latitude and longitude.

  • Steps: I set up a call to the Google Distance Matrix API on a JavaScript Query.

  • Details: I used the same call on Postman and it works fine. I', also using the Geocode API and it works fine. My API key is unrestricted. I get a CORS error on the browser's when running on retool. Tried using https://cors-anywhere.herokuapp.com/ but is not working. Any idea if there is a specific issue with the distance matrix api?

  • Screenshots:

Update: I manage to call the API using a REST API query because of how I process the response I need the API call to be part of the JavaScript query.

1 Like

Thank for sharing your solution Woakin! :raised_hands: Would you mind sharing details or screenshots for anyone else that may run into this in the future?

Here is a screenshot of how to make the call using the REST API resource. In this example, I use the basic call structure with the parameters: units, origins, destinations, and key. I retrieve the origin's latitude and longitude dynamically with another query called geocodePostalCodes.

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins={{ geocodePostalCodes.data[0].lat }},{{ geocodePostalCodes.data[0].lng }}&destinations=19.3674396,-99.2617932&key=xxxxxxxxxxxxx