Home > Blockchain >  Fetch Location using google Api
Fetch Location using google Api

Time:03-02

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:

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?

CodePudding user response:

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)
  • Related