lundi 30 septembre 2019

how to get perfectly data from google-api

I am trying to get data from google places API ( written on JSON ). Each result has a single page with the most 20 results. if there are more results you will have var called Next_token. Next token is a link to the next page with the results. if there isn't so it's not declared. I tried to make so stuff with no 100% success.

I am a bit new with the ES5/6 on JS. So I tried to fetch the first page and after that looping until there aren't more results. I the code I cut all that doesn't matter.

function getdatafirsttry(url)
{
    fetch(url,{method: 'get'})
    .then(response =>
        response.json())
    .then(data => {
    next_token = data.next_page_token;

    if(next_token !== undefined){
    url='https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=32.0333332,34.7666636&radius=1500&type=restaurant&key=AIzaSyCbaehPkHsIMdyfCjDRdfMlZHSVrl12E_A&fields=opening_hours,photos,rating.json&pagetoken='+next_token;
    toContinue = true;
    }
    else{toContinue=false}



    }

//Next page token
function alltogheter(url)
{

    getdatafirsttry(url);

}
getdatafirsttry(url);



Aucun commentaire:

Enregistrer un commentaire