Explorar o código

Edited MapScreen to drop pins for each event.

Fernando Diaz %!s(int64=9) %!d(string=hai) anos
pai
achega
f8af42c80d

+ 77 - 0
app/src/main/java/com/example/yiupang/freefoodfinder/MapScreen.java

@@ -2,10 +2,15 @@ package com.example.yiupang.freefoodfinder;
 
 import android.os.Bundle;
 import android.support.annotation.Nullable;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.ListView;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.OnMapReadyCallback;
@@ -14,6 +19,11 @@ import com.google.android.gms.maps.model.BitmapDescriptorFactory;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.MarkerOptions;
 
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.util.List;
+import java.util.ArrayList;
+
 /**
  * Created by yiupang on 5/6/2017.
  *
@@ -21,6 +31,11 @@ import com.google.android.gms.maps.model.MarkerOptions;
  */
 public class MapScreen extends android.support.v4.app.Fragment implements OnMapReadyCallback
 {
+    ArrayList<Double> lats = new ArrayList<Double>();
+    ArrayList<Double> lngs = new ArrayList<Double>();
+    ArrayList<String> titles = new ArrayList<String>();
+    ArrayList<String> descrips = new ArrayList<String>();
+
     @Override
     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
     {
@@ -28,12 +43,64 @@ public class MapScreen extends android.support.v4.app.Fragment implements OnMapR
         android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager();
         SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
         mapFragment.getMapAsync(this);
+
+        //
+        HttpCall httpCall = new HttpCall();
+        httpCall.setMethodType(HttpCall.GET);
+        httpCall.setUrl("http://free-food-finder.herokuapp.com/events");
+
+        new HttpRequest() {
+
+            @Override
+            public void onResponse(Object response, int code) {
+                super.onResponse(response, code);
+                if (code != HttpURLConnection.HTTP_OK) {
+                    /* Error Handling */
+                } else {
+                    ObjectMapper mapper = new ObjectMapper();
+                    TypeFactory typeFactory = mapper.getTypeFactory();
+                    List<Event> events = null;/*Parse to Event Objs*/
+                    try {
+
+                        events = mapper.reader(
+                                typeFactory.constructCollectionType(List.class, Event.class)
+                        ).readValue((JsonNode) response);
+
+                        for(int i = 0; i < events.size(); i++)
+                        {
+                            Event currEvent = events.get(i);
+
+                            double lat = currEvent.getLat();
+                            double lng = currEvent.getLng();
+                            String title = currEvent.getName();
+                            String desc = currEvent.getDescription();
+
+                            lats.add(lat);
+                            lngs.add(lng);
+                            titles.add(title);
+                            descrips.add(desc);
+
+                        }
+
+                    } catch (IOException e) {
+                        /*handle error*/
+                        Log.d("size:  ", e + "");
+                    }
+
+                }
+            }
+
+        }.execute(httpCall);
+
         return view;
     }
 
     @Override
     public void onMapReady(GoogleMap map)
     {
+
+        /*
+
         // Add a marker in Sydney and move the camera
         LatLng slo = new LatLng(35.2827778, -120.6586111);
         LatLng sf = new LatLng(37.774929, -122.419416);
@@ -42,5 +109,15 @@ public class MapScreen extends android.support.v4.app.Fragment implements OnMapR
         map.addMarker(new MarkerOptions().position(theP).title("The P").snippet("The “P” is a 50-by-30 foot landmark located atop a northwestern hill of California Polytechnic State University, San Luis Obispo, California."));
         map.addMarker(new MarkerOptions().position(sf).title("Marker in sf").snippet("Golden Gate Bridge"));
         map.moveCamera(CameraUpdateFactory.newLatLngZoom(slo, 12.0f));
+
+        */
+
+        for(int i = 0; i < lats.size(); i++)
+        {
+            LatLng currPos = new LatLng(lats.get(i), lngs.get(i));
+
+            map.addMarker(new MarkerOptions().position(currPos).title(titles.get(i)).snippet(descrips.get(i)));
+
+        }
     }
 }

+ 45 - 44
app/src/main/res/layout/activity_event_details_screen1.xml

@@ -2,109 +2,110 @@
 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:columnCount="2"
+    android:rowCount="6"
     android:orientation="vertical">
 
 
     <TextView
         android:id="@+id/creatorlabel"
-        android:layout_width="80dp"
-        android:layout_height="wrap_content"
-        android:ems="10"
-        android:inputType="textPersonName"
+        android:layout_column="0"
+        android:layout_row="0"
+        android:typeface="serif"
+        android:textStyle="bold"
+        android:padding="20dp"
         android:text="Creator:" />
 
     <TextView
         android:id="@+id/datelabel"
-        android:layout_width="66dp"
-        android:layout_height="wrap_content"
-        android:ems="10"
-        android:inputType="textPersonName"
+        android:layout_column="0"
+        android:layout_row="1"
+        android:typeface="serif"
+        android:textStyle="bold"
+        android:padding="20dp"
         android:text="Date:" />
 
     <TextView
         android:id="@+id/timelabel"
-        android:layout_width="68dp"
-        android:layout_height="wrap_content"
-        android:ems="10"
-        android:inputType="textPersonName"
+        android:layout_column="0"
+        android:layout_row="2"
+        android:typeface="serif"
+        android:textStyle="bold"
+        android:padding="20dp"
         android:text="Time:" />
 
     <TextView
         android:id="@+id/placelabel"
-        android:layout_width="70dp"
-        android:layout_height="wrap_content"
-        android:ems="10"
-        android:inputType="textPersonName"
+        android:layout_column="0"
+        android:layout_row="3"
+        android:typeface="serif"
+        android:textStyle="bold"
+        android:padding="20dp"
         android:text="Place:" />
 
     <TextView
         android:id="@+id/foodlabel"
-        android:layout_width="115dp"
-        android:layout_height="wrap_content"
-        android:ems="10"
-        android:inputType="textPersonName"
+        android:layout_column="0"
+        android:layout_row="4"
+        android:typeface="serif"
+        android:textStyle="bold"
+        android:padding="20dp"
         android:text="Food Served:" />
 
     <TextView
         android:id="@+id/desclabel"
-        android:layout_width="105dp"
-        android:layout_height="wrap_content"
-        android:ems="10"
-        android:inputType="textPersonName"
+        android:layout_column="0"
+        android:layout_row="5"
+        android:textStyle="bold"
+        android:padding="20dp"
         android:text="Description:" />
 
     <TextView
         android:id="@+id/nameText"
-        android:layout_width="255dp"
-        android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="0"
-        android:ems="10"
+        android:typeface="serif"
+        android:padding="20dp"
         android:inputType="textPersonName" />
 
     <TextView
         android:id="@+id/timeText"
-        android:layout_width="255dp"
-        android:layout_height="wrap_content"
         android:layout_column="1"
-        android:layout_row="2"
-        android:ems="10"
+        android:layout_row="1"
+        android:typeface="serif"
+        android:padding="20dp"
         android:inputType="textPersonName" />
 
     <TextView
         android:id="@+id/datetext"
-        android:layout_width="255dp"
-        android:layout_height="wrap_content"
         android:layout_column="1"
-        android:layout_row="1"
-        android:ems="10"
+        android:layout_row="2"
+        android:typeface="serif"
+        android:padding="20dp"
         android:inputType="textPersonName" />
 
     <TextView
         android:id="@+id/placeText"
-        android:layout_width="255dp"
-        android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="3"
-        android:ems="10"
+        android:typeface="serif"
+        android:padding="20dp"
         android:inputType="textPersonName" />
 
     <TextView
         android:id="@+id/foodText"
-        android:layout_width="255dp"
-        android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="4"
-        android:ems="10"
+        android:typeface="serif"
+        android:padding="20dp"
         android:inputType="textPersonName" />
 
     <TextView
         android:id="@+id/descText"
-        android:layout_width="255dp"
-        android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="5"
-        android:ems="10"
+        android:typeface="serif"
+        android:padding="20dp"
         android:inputType="textPersonName" />