Skip to content

Commit

Permalink
#45 Showing Field Papers title and page number that the center of the…
Browse files Browse the repository at this point in the history
… map is in
  • Loading branch information
hallahan committed Apr 21, 2016
1 parent 9b4fb89 commit 3470e18
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
40 changes: 38 additions & 2 deletions MapboxAndroidSDK/src/main/java/org/fieldpapers/model/FPAtlas.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import com.mapbox.mapboxsdk.events.RotateEvent;
import com.mapbox.mapboxsdk.events.ScrollEvent;
import com.mapbox.mapboxsdk.events.ZoomEvent;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.overlay.Marker;
import com.mapbox.mapboxsdk.views.MapView;
import com.mapbox.mapboxsdk.views.MapViewListener;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.index.quadtree.Quadtree;

import org.apache.commons.io.FileUtils;
Expand All @@ -23,18 +28,22 @@
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FPAtlas implements MapViewListener, MapListener {

private static final String PREVIOUS_FP_FILE_PATH = "org.redcross.openmapkit.PREVIOUS_FP_FILE_PATH";
public static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory();

private static File fpGeoJson;
private static FPAtlas atlas;

private JSONObject geoJson;
private String title;

private Activity activity;
private MapView mapView;

private Quadtree spatialIndex = new Quadtree();
private Map<String, FPPage> pages = new HashMap<>();
Expand Down Expand Up @@ -65,6 +74,7 @@ public static void addToMap(Activity activity, MapView mapView) throws IOExcepti
if (atlas == null) return;

atlas.setActivity(activity);
atlas.setMapView(mapView);

mapView.setMapViewListener(atlas);
mapView.addListener(atlas);
Expand Down Expand Up @@ -121,11 +131,37 @@ public void setActivity(Activity activity) {
this.activity = activity;
}

private FPPage findMapCenterPage() {
public void setMapView(MapView mapView) {
this.mapView = mapView;
}

return null;
private void findMapCenterPage() {
LatLng center = mapView.getCenter();
Coordinate centerCoord = new Coordinate(center.getLongitude(), center.getLatitude());
Envelope env = new Envelope(centerCoord);
List fpPages = spatialIndex.query(env);
for (Object p : fpPages) {
FPPage page = (FPPage)p;
Geometry pageGeom = page.geometry();
if (pageGeom.contains(GEOMETRY_FACTORY.createPoint(centerCoord))) {
foundMapCenterPage(page);
}
}
}

private void foundMapCenterPage(FPPage page) {
if (activity != null && activity instanceof FPListener) {
String msg = pageMessage(page);
((FPListener)activity).onMapCenterPageChangeMessage(msg);
}
}

private String pageMessage(FPPage page) {
return title() + " " + page.pageNumber();
}




/**
* LISTENERS
Expand Down
16 changes: 11 additions & 5 deletions MapboxAndroidSDK/src/main/java/org/fieldpapers/model/FPPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public class FPPage {
private String pageNumber;
private String url;

private GeometryFactory geometryFactory = new GeometryFactory();

public FPPage(JSONObject feature) {
this.feature = feature;
parsePageNumber();
buildGeometry();
buildEnvelope();
}
Expand All @@ -37,7 +36,15 @@ private void buildGeometry() {
double lat = coord.getDouble(1);
coordinates[i] = new Coordinate(lng, lat);
}
geom = geometryFactory.createPolygon(coordinates);
geom = FPAtlas.GEOMETRY_FACTORY.createPolygon(coordinates);
} catch (JSONException e) {
e.printStackTrace();
}
}

private void parsePageNumber() {
try {
pageNumber = feature.getJSONObject("properties").getString("page_number");
} catch (JSONException e) {
e.printStackTrace();
}
Expand All @@ -48,8 +55,7 @@ private void buildEnvelope() {
}

public String pageNumber() {

return null;
return pageNumber;
}

public Geometry geometry() {
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/org/redcross/openmapkit/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class MapActivity extends AppCompatActivity implements OSMSelectionListen

protected MapView mapView;
protected OSMMap osmMap;
protected TextView fieldPapersMsg;
protected ListView mTagListView;
protected ImageButton mCloseListViewButton;
protected ImageButton tagButton;
Expand Down Expand Up @@ -141,6 +142,9 @@ protected void onCreate(Bundle savedInstanceState) {

//get map from layout
mapView = (MapView)findViewById(R.id.mapView);

// get Field Papers Message
fieldPapersMsg = (TextView)findViewById(R.id.fieldPapersMsg);

// initialize basemap object
basemap = new Basemap(this);
Expand Down Expand Up @@ -809,7 +813,7 @@ private boolean isAppInstalled(String uri) {

@Override
public void onMapCenterPageChangeMessage(String msg) {

fieldPapersMsg.setText(msg);
}

}
12 changes: 11 additions & 1 deletion app/src/main/res/layout/activity_map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/fieldPapersMsg"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 3470e18

Please sign in to comment.