GDes00
GDes00

Reputation: 285

empty json from google maps api url fetch

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.

Upvotes: 0

Views: 310

Answers (1)

Dhananjai Pai
Dhananjai Pai

Reputation: 6015

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

Upvotes: 1

Related Questions