Armax / Pokemon-GO-node-api

Pokemon GO api node.js library
MIT License
875 stars 198 forks source link

can someone explain me how to get gyms and pokestops data #246

Closed uzaif313 closed 7 years ago

uzaif313 commented 7 years ago

I have init user with google account but find a way to collect data of gyms and pokestops

ameyer commented 7 years ago

What data do you want?

Something like this will check the forts (pokestops) around you and get the content from them if you are close enough


function heartbeat(){

    pokeAPI.Heartbeat(function(err,hb) {
        var forts = [];

        for (var i = hb.cells.length - 1; i >= 0; i--) {
            var cell = hb.cells[i];
            forts.push(cell.Fort);
        }

        checkForts(forts); //Check if we are close to any forts
    });
}

function checkForts(fortCells){
    var needToBreak = false;

    for(var cellIndex = 0; cellIndex < fortCells.length; cellIndex ++){

        var forts = fortCells[cellIndex];

        //scan all forst and see if any are close enough to get
        for (var fort_i = 0; fort_i < forts.length; fort_i++) {
            var fort = forts[fort_i];

            var fortLocation = {'latitude': fort.Latitude, 'longitude': fort.Longitude};
            var ourPosition = {'latitude': newLocation.coords.latitude, 'longitude': newLocation.coords.longitude};

            var distanceToFort = distance(ourPosition,fortLocation);
            var fortID = fort.FortId;
            var fortLat = fort.Latitude;
            var fortLong = fort.Longitude;

            //0.0248548 is the max distance we can be to go to a fort
            //fort.FortType 1 is a pokestop - 0 is a gym
            if(distanceToFort < 0.0248548 && fort.FortType == 1 && fort.CooldownCompleteMs == null){
                console.log("\t===APPROCHING FORT===");

                pokeAPI.GetFort(fortID, fortLat, fortLong, function(err, data){
                    if(err) printError("check forts",err);

                    if(data.result == 1){

                        for(var itemIndex = 0; itemIndex < data.items_awarded.length; itemIndex ++){

                            var item = data.items_awarded[itemIndex];
                            var itemID = item.item_id;                          
                            var itemName = itemNames[itemID];
                            var itemCountAddition = item.item_count;

                            console.log('we got '+item.item_count+'x '+itemName+'s');

                        }
                    }else{
                        var reason = ["NO RESULT SET", "SUCCESS", "OUT OF RANGE", "IN COOLDOWN PERIOD", "INVENTORY FULL"];
                        console.log(reason[data.result]);
                    }
                });

                needToBreak = true;
                break; //break the loop, we have a fort to enter, no need to continue
            }
        }

        if(needToBreak){
            break;
        }

    }
}

function distance(point1, point2) {
    ////////////////////////////////////////////////
    //Figure out how many miles between points
    ////////////////////////////////////////////////
    var earthRadius = 6371; // radius of the earth in km
    var dLat = toRad(point2['latitude']-point1['latitude']);
    var dLon = toRad(point2['longitude']-point1['longitude']);
    var lat1 = toRad(point1['latitude']);
    var lat2 = toRad(point2['latitude']);

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
    var angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var distanceBetweenPoints = earthRadius * angle;
    return distanceBetweenPoints / 0.621371; //convert to miles
}
uzaif313 commented 7 years ago

Thanks for response @ameyer