use of com.pokegoapi.api.map.pokemon.NearbyPokemon in project PokeGOAPI-Java by Grover-c13.
the class CatchPokemonAtAreaExample method main.
/**
* Catches a pokemon at an area.
*
* @param args args
*/
public static void main(String[] args) {
OkHttpClient http = new OkHttpClient();
final PokemonGo api = new PokemonGo(http);
try {
HashProvider hasher = ExampleConstants.getHashProvider();
api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
// Catch all pokemon in the current area
catchArea(api);
MapObjects mapObjects = api.getMap().mapObjects;
// Find all pokestops with pokemon nearby
List<Pokestop> travelPokestops = new ArrayList<>();
Set<NearbyPokemon> nearby = mapObjects.nearby;
for (NearbyPokemon nearbyPokemon : nearby) {
String fortId = nearbyPokemon.getFortId();
// Check if nearby pokemon is near a pokestop
if (fortId != null && fortId.length() > 0) {
// Find the pokestop with the fort id of the nearby pokemon
Pokestop pokestop = mapObjects.getPokestop(fortId);
if (pokestop != null && !travelPokestops.contains(pokestop)) {
travelPokestops.add(pokestop);
}
}
}
// Sort from closest to farthest
Collections.sort(travelPokestops, new Comparator<Pokestop>() {
@Override
public int compare(Pokestop primary, Pokestop secondary) {
double lat = api.latitude;
double lng = api.longitude;
double distance1 = MapUtil.distFrom(primary.getLatitude(), primary.getLongitude(), lat, lng);
double distance2 = MapUtil.distFrom(secondary.getLatitude(), secondary.getLongitude(), lat, lng);
return Double.compare(distance1, distance2);
}
});
for (Pokestop pokestop : travelPokestops) {
Point destination = new Point(pokestop.getLatitude(), pokestop.getLongitude());
// Use the current player position as the source and the pokestop position as the destination
// Travel to Pokestop at 20KMPH
Path path = new Path(api.getPoint(), destination, 20.0);
System.out.println("Traveling to " + destination + " at 20KMPH!");
path.start(api);
try {
while (!path.complete) {
// Calculate the desired intermediate point for the current time
Point point = path.calculateIntermediate(api);
// Set the API location to that point
api.setLatitude(point.getLatitude());
api.setLongitude(point.getLongitude());
// Sleep for 2 seconds before setting the location again
Thread.sleep(2000);
}
} catch (InterruptedException e) {
break;
}
System.out.println("Finished traveling to pokestop, catching pokemon.");
catchArea(api);
}
} catch (NoSuchItemException | RequestFailedException e) {
Log.e("Main", "An exception occurred while running example: ", e);
}
}
Aggregations