Skip to content

Commit

Permalink
Searching pokemons (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkOut authored and omkarmoghe committed Jul 23, 2016
1 parent 312a9a6 commit 7c51936
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ dependencies {
compile 'com.google.android.gms:play-services-maps:9.2.1'
compile 'com.google.android.gms:play-services-location:9.2.1'

//Pokemon Go API - Got double pokemon bug
//compile 'com.github.Grover-c13:PokeGOAPI-Java:4f5b715380'

//Pokemon Go API
compile 'com.github.Grover-c13:PokeGOAPI-Java:4f5b715380'
compile 'com.github.Grover-c13:PokeGOAPI-Java:2e14fab7f0'

//Network
compile 'com.github.franmontiel:PersistentCookieJar:v0.9.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.omkarmoghe.pokemap.models.events;

import com.google.android.gms.maps.model.LatLng;

/**
* Created by Link on 23/07/2016.
*/
public class SearchInPosition {

private LatLng position;

public LatLng getPosition() {
return position;
}

public void setPosition(LatLng position) {
this.position = position;
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/omkarmoghe/pokemap/views/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.google.android.gms.maps.model.LatLng;
import com.omkarmoghe.pokemap.R;
import com.omkarmoghe.pokemap.models.events.LoginEventResult;
import com.omkarmoghe.pokemap.models.events.SearchInPosition;
import com.omkarmoghe.pokemap.models.events.ServerUnreachableEvent;
import com.omkarmoghe.pokemap.models.events.TokenExpiredEvent;
import com.omkarmoghe.pokemap.views.login.RequestCredentialsDialogFragment;
Expand Down Expand Up @@ -127,6 +128,17 @@ public void onEvent(LoginEventResult result) {
}
}

/**
* Called whenever a use whats to search pokemons on a different position
*
* @param event PoJo with LatLng obj
*/
@Subscribe
public void onEvent(SearchInPosition event) {
Toast.makeText(this, "Searching...", Toast.LENGTH_LONG).show();
nianticManager.getCatchablePokemon(event.getPosition().latitude, event.getPosition().longitude, 0D);
}

/**
* Called whenever a ServerUnreachableEvent is posted to the bus. Posted when the server cannot be reached
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.omkarmoghe.pokemap.R;
import com.omkarmoghe.pokemap.controllers.map.LocationManager;
import com.omkarmoghe.pokemap.models.events.CatchablePokemonEvent;
import com.omkarmoghe.pokemap.models.events.SearchInPosition;
import com.pokegoapi.api.map.pokemon.CatchablePokemon;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand All @@ -39,6 +43,7 @@
* create an instance of this fragment.
*/
public class MapWrapperFragment extends Fragment implements OnMapReadyCallback,
GoogleMap.OnMapLongClickListener,
ActivityCompat.OnRequestPermissionsResultCallback {

private static final int LOCATION_PERMISSION_REQUEST = 19;
Expand All @@ -49,6 +54,10 @@ public class MapWrapperFragment extends Fragment implements OnMapReadyCallback,
private SupportMapFragment mSupportMapFragment;
private GoogleMap mGoogleMap;
private Location mLocation = null;
private Marker userSelectedPositionMarker = null;
private Circle userSelectedPositionCircle = null;
private List<Marker> markerList = new ArrayList<>();

public MapWrapperFragment() {
// Required empty public constructor
}
Expand Down Expand Up @@ -137,15 +146,26 @@ private void initMap(){

private void setPokemonMarkers(final List<CatchablePokemon> pokeList){
if (mGoogleMap != null) {
//Removing all pokemons from map
if (markerList != null && !markerList.isEmpty()){
for(Marker marker : markerList){
marker.remove();
}
markerList = new ArrayList<Marker>(); //cleaning the array
}

for (CatchablePokemon poke : pokeList) {
int resourceID = getResources().getIdentifier("p" + poke.getPokemonId().getNumber(), "drawable", getActivity().getPackageName());
long millisLeft = poke.getExpirationTimestampMs() - System.currentTimeMillis();
Marker marker = mGoogleMap.addMarker(new MarkerOptions()
.position(new LatLng(poke.getLatitude(), poke.getLongitude()))
.title(poke.getPokemonId().name())
.snippet("Dissapears in: " + getDurationBreakdown(poke.getExpirationTimestampMs()))
.snippet("Dissapears in: " + getDurationBreakdown(millisLeft))
.icon(BitmapDescriptorFactory.fromResource(resourceID)));

marker.showInfoWindow();
//adding pokemons to list to be removed on next search
markerList.add(marker);
}
} else {
Toast.makeText(getContext(), "The map is not initialized.", Toast.LENGTH_LONG).show();
Expand Down Expand Up @@ -215,9 +235,43 @@ public void onMapReady(GoogleMap googleMap) {
settings.setCompassEnabled(true);
settings.setTiltGesturesEnabled(true);
settings.setMyLocationButtonEnabled(false);
//Handle long click
mGoogleMap.setOnMapLongClickListener(this);
//Disable for now coz is under FAB
settings.setMapToolbarEnabled(false);
initMap();
}

@Override
public void onMapLongClick(LatLng position) {
//Draw user position marker with circle
drawMarkerWithCircle (position);

//Sending event to MainActivity
SearchInPosition sip = new SearchInPosition();
sip.setPosition(position);
EventBus.getDefault().post(sip);
}

private void drawMarkerWithCircle(LatLng position){
//Check and eventually remove old marker
if(userSelectedPositionMarker != null && userSelectedPositionCircle != null){
userSelectedPositionMarker.remove();
userSelectedPositionCircle.remove();
}

double radiusInMeters = 100.0;
int strokeColor = 0xff3399FF; // outline
int shadeColor = 0x4400CCFF; // fill

CircleOptions circleOptions = new CircleOptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8);
userSelectedPositionCircle = mGoogleMap.addCircle(circleOptions);

userSelectedPositionMarker = mGoogleMap.addMarker(new MarkerOptions()
.position(position)
.title("Position Picked")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
}

}

18 changes: 18 additions & 0 deletions app/src/main/res/layout/fragment_map_wrapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@
android:layout_margin="10dp"
android:id="@+id/location_fab" />

<FrameLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center_horizontal|top"
android:background="#4D000000">

<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search_pokemon_map_suggestion"
android:id="@+id/txtSuggestions"
android:layout_gravity="center"
android:textColor="#ffffff"
android:textSize="12sp"
android:textStyle="italic" />
</FrameLayout>

</FrameLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@
<string name="login_warning_title">Warning:</string>

<string name="request_credentials_title">Insert your Pokemon Go credentials</string>
<string name="search_pokemon_map_suggestion">(Long press on map to change searching position)</string>

</resources>

0 comments on commit 7c51936

Please sign in to comment.