From 1bc1d884640afd22c1779af4e9f155ada07edaf2 Mon Sep 17 00:00:00 2001
From: Meibes <26258451+Meibes@users.noreply.github.com>
Date: Sat, 19 Dec 2020 12:06:46 +0100
Subject: [PATCH] Android Samples: add Fragment example (#796)
---
vtm-android-example/AndroidManifest.xml | 3 +
.../res/layout/fragment_blank.xml | 12 ++
.../res/layout/fragment_map.xml | 3 +-
.../res/menu/fragment_menu.xml | 9 ++
vtm-android-example/res/values/strings.xml | 2 +
.../org/oscim/android/test/BlankFragment.java | 38 ++++++
.../oscim/android/test/FragmentActivity.java | 60 +++++++++
.../org/oscim/android/test/MapFragment.java | 119 ++++++++++++++++++
.../src/org/oscim/android/test/Samples.java | 2 +-
9 files changed, 246 insertions(+), 2 deletions(-)
create mode 100644 vtm-android-example/res/layout/fragment_blank.xml
create mode 100644 vtm-android-example/res/menu/fragment_menu.xml
create mode 100644 vtm-android-example/src/org/oscim/android/test/BlankFragment.java
create mode 100644 vtm-android-example/src/org/oscim/android/test/FragmentActivity.java
create mode 100644 vtm-android-example/src/org/oscim/android/test/MapFragment.java
diff --git a/vtm-android-example/AndroidManifest.xml b/vtm-android-example/AndroidManifest.xml
index 77a777ec..9e6be991 100644
--- a/vtm-android-example/AndroidManifest.xml
+++ b/vtm-android-example/AndroidManifest.xml
@@ -34,6 +34,9 @@
+
diff --git a/vtm-android-example/res/layout/fragment_blank.xml b/vtm-android-example/res/layout/fragment_blank.xml
new file mode 100644
index 00000000..dea2782e
--- /dev/null
+++ b/vtm-android-example/res/layout/fragment_blank.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
diff --git a/vtm-android-example/res/layout/fragment_map.xml b/vtm-android-example/res/layout/fragment_map.xml
index 5b7297cc..30128a8e 100644
--- a/vtm-android-example/res/layout/fragment_map.xml
+++ b/vtm-android-example/res/layout/fragment_map.xml
@@ -1,9 +1,10 @@
+
-
diff --git a/vtm-android-example/res/menu/fragment_menu.xml b/vtm-android-example/res/menu/fragment_menu.xml
new file mode 100644
index 00000000..71c75689
--- /dev/null
+++ b/vtm-android-example/res/menu/fragment_menu.xml
@@ -0,0 +1,9 @@
+
+
diff --git a/vtm-android-example/res/values/strings.xml b/vtm-android-example/res/values/strings.xml
index f637fdd1..d4757468 100644
--- a/vtm-android-example/res/values/strings.xml
+++ b/vtm-android-example/res/values/strings.xml
@@ -28,5 +28,7 @@
Warning
To run this sample activity, you need an MBTiles database installed on storage.\n\nadb push %s %s
Exit
+ Replace fragment
+ This is a fragment to test the back stack behaviour of the map fragment.
diff --git a/vtm-android-example/src/org/oscim/android/test/BlankFragment.java b/vtm-android-example/src/org/oscim/android/test/BlankFragment.java
new file mode 100644
index 00000000..ae175cce
--- /dev/null
+++ b/vtm-android-example/src/org/oscim/android/test/BlankFragment.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2020 Meibes
+ *
+ * This program is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program. If not, see .
+ */
+package org.oscim.android.test;
+
+import android.app.Fragment;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+@SuppressWarnings("deprecation")
+public class BlankFragment extends Fragment {
+
+ static BlankFragment newInstance() {
+ BlankFragment instance = new BlankFragment();
+
+ Bundle args = new Bundle();
+ instance.setArguments(args);
+ return instance;
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+ return inflater.inflate(R.layout.fragment_blank, container, false);
+ }
+}
diff --git a/vtm-android-example/src/org/oscim/android/test/FragmentActivity.java b/vtm-android-example/src/org/oscim/android/test/FragmentActivity.java
new file mode 100644
index 00000000..8e0a63a2
--- /dev/null
+++ b/vtm-android-example/src/org/oscim/android/test/FragmentActivity.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2020 Meibes
+ *
+ * This program is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program. If not, see .
+ */
+package org.oscim.android.test;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.os.Bundle;
+import android.view.Menu;
+import android.view.MenuItem;
+
+public class FragmentActivity extends Activity {
+
+ @SuppressWarnings("deprecation")
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_map_fragment);
+
+ setTitle(getClass().getSimpleName());
+
+ if (savedInstanceState == null) {
+ Fragment mapFragment = MapFragment.newInstance();
+ FragmentTransaction ft = getFragmentManager().beginTransaction();
+ ft.add(R.id.fragment_container, mapFragment).commit();
+ }
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getMenuInflater().inflate(R.menu.fragment_menu, menu);
+ return true;
+ }
+
+ @SuppressWarnings("deprecation")
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.replace_fragment) {
+ Fragment blankFragment = BlankFragment.newInstance();
+ FragmentTransaction ft = getFragmentManager().beginTransaction();
+ ft.replace(R.id.fragment_container, blankFragment);
+ ft.addToBackStack(null);
+ ft.commit();
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+}
diff --git a/vtm-android-example/src/org/oscim/android/test/MapFragment.java b/vtm-android-example/src/org/oscim/android/test/MapFragment.java
new file mode 100644
index 00000000..327fc651
--- /dev/null
+++ b/vtm-android-example/src/org/oscim/android/test/MapFragment.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2018-2020 devemux86
+ * Copyright 2020 Meibes
+ *
+ * This program is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program. If not, see .
+ */
+package org.oscim.android.test;
+
+import android.app.Fragment;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.RelativeLayout;
+import org.oscim.android.MapView;
+import org.oscim.backend.CanvasAdapter;
+import org.oscim.layers.tile.buildings.BuildingLayer;
+import org.oscim.layers.tile.vector.VectorTileLayer;
+import org.oscim.layers.tile.vector.labeling.LabelLayer;
+import org.oscim.renderer.GLViewport;
+import org.oscim.scalebar.DefaultMapScaleBar;
+import org.oscim.scalebar.MapScaleBar;
+import org.oscim.scalebar.MapScaleBarLayer;
+import org.oscim.theme.VtmThemes;
+import org.oscim.tiling.source.mapfile.MapFileTileSource;
+
+import java.io.File;
+
+/**
+ * You'll need a map with filename berlin.map from download.mapsforge.org in device storage.
+ */
+@SuppressWarnings("deprecation")
+public class MapFragment extends Fragment {
+
+ private MapView mapView;
+
+ public static MapFragment newInstance() {
+ MapFragment instance = new MapFragment();
+
+ Bundle args = new Bundle();
+ instance.setArguments(args);
+ return instance;
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+ return inflater.inflate(R.layout.fragment_map, container, false);
+ }
+
+ @Override
+ public void onViewCreated(View view, Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+
+ mapView = new MapView(getActivity());
+ RelativeLayout relativeLayout = view.findViewById(R.id.mapView);
+ relativeLayout.addView(mapView);
+
+ // Tile source
+ MapFileTileSource tileSource = new MapFileTileSource();
+ File file = new File(getActivity().getExternalFilesDir(null), "berlin.map");
+ tileSource.setMapFile(file.getAbsolutePath());
+
+ // Vector layer
+ VectorTileLayer tileLayer = mapView.map().setBaseMap(tileSource);
+
+ // Building layer
+ mapView.map().layers().add(new BuildingLayer(mapView.map(), tileLayer));
+
+ // Label layer
+ mapView.map().layers().add(new LabelLayer(mapView.map(), tileLayer));
+
+ // Render theme
+ mapView.map().setTheme(VtmThemes.DEFAULT);
+
+ // Scale bar
+ MapScaleBar mapScaleBar = new DefaultMapScaleBar(mapView.map());
+ MapScaleBarLayer mapScaleBarLayer = new MapScaleBarLayer(mapView.map(), mapScaleBar);
+ mapScaleBarLayer.getRenderer().setPosition(GLViewport.Position.BOTTOM_LEFT);
+ mapScaleBarLayer.getRenderer().setOffset(5 * CanvasAdapter.getScale(), 0);
+ mapView.map().layers().add(mapScaleBarLayer);
+
+ // Note: this map position is specific to Berlin area
+ mapView.map().setMapPosition(52.517037, 13.38886, 1 << 12);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ if (mapView != null) {
+ mapView.onResume();
+ }
+ }
+
+ @Override
+ public void onPause() {
+ if (mapView != null) {
+ mapView.onPause();
+ }
+ super.onPause();
+ }
+
+ @Override
+ public void onDestroyView() {
+ if (mapView != null) {
+ mapView.onDestroy();
+ mapView = null;
+ }
+ super.onDestroyView();
+ }
+}
diff --git a/vtm-android-example/src/org/oscim/android/test/Samples.java b/vtm-android-example/src/org/oscim/android/test/Samples.java
index b084be64..83c153cf 100644
--- a/vtm-android-example/src/org/oscim/android/test/Samples.java
+++ b/vtm-android-example/src/org/oscim/android/test/Samples.java
@@ -28,7 +28,6 @@ package org.oscim.android.test;
import android.app.Activity;
import android.content.Intent;
-import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
@@ -126,5 +125,6 @@ public class Samples extends Activity {
linearLayout.addView(createButton(GdxPoi3DActivity.class));
linearLayout.addView(createButton(OverpassActivity.class));
linearLayout.addView(createButton(ClusterMarkerOverlayActivity.class));
+ linearLayout.addView(createButton(FragmentActivity.class));
}
}