0

I am using google maps API to get a city name from coordinates. When I fetch the API url I get an empty json

unction callWCoords(position) {
    let latitude = position.coords.latitude;
    let longitude =position.coords.longitude;
    let call1;
    let API1="https://maps.googleapis.com/maps/api/geocode/json?latlng=+"+latitude+","+longitude+"&key=AIzaSyAfH3Ypu0Al8HpNRXhOPEzGLeNbkxOlsoI"
    fetch(API1).then(
        response=>{alert(JSON.stringify(response));

        }).then( data=>{

    })
    //getPlaceC(call1);

}

JSON.stringfy(response) returns "{}" as well if I try JSON.stringfy(data) in the second .then block. Since in the official google json I should get an array called results I tried to substitute results with data but it didn't work.

1 Answer 1

1
function callWCoords(position) {
    let latitude = position.coords.latitude;
    let longitude =position.coords.longitude;
    let API1="https://maps.googleapis.com/maps/api/geocode/json?latlng=+"+latitude+","+longitude+"&key=AIzaSyAfH3Ypu0Al8HpNRXhOPEzGLeNbkxOlsoI"
    fetch(API1).then(res => res.json()).then(data => console.log(data))
}

You must call the res.json() function on the fetch return to get the data. Also, you are not returning the data back after alert, which is basically removing the data from the next .then

Sign up to request clarification or add additional context in comments.

1 Comment

I hadn't nested it, I had put it inside the navigator.geolocation.getCurrentPosition() to test it locally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.