Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetch Location using google Api

I am trying to get the latitude and longitude of user location using google api

I have tried doing this using the fetch method in javascript but I do not get any response. There’s no error or anything.
The documentation says to fetch it this way:
https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY

How I fetch it:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const res = await fetch(https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY)
const response = await res.json()

When I first did it like above I got an error because it returned nothing which I found out after console.logging res.text()
I do not know what I’m doing wrong In the documentation they equally said something about including Request body but it’s optional, So I have no need to include it. Please do you have any ideas on what I’m doing wrong?

>Solution :

I believe you need to send this as a POST request according to the documentation you linked. By default fetch sends a GET request.

To do that you just need to pass in the request type to the fetch function

const response = await fetch(<URI>,{method:'POST'});
const res =  await response.json()
console.log(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading