Skip to content

Commit

Permalink
minor fragment example changes (#1094)
Browse files Browse the repository at this point in the history
- split map and theme load
- add toasts
- ...
  • Loading branch information
fm-sys authored Jan 17, 2024
1 parent 5ccd645 commit 81b45f3
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions vtm-android-example/src/org/oscim/android/test/MapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import org.oscim.android.MapView;
import org.oscim.backend.CanvasAdapter;
import org.oscim.core.MapPosition;
Expand Down Expand Up @@ -55,7 +57,6 @@ public class MapFragment extends android.app.Fragment implements XmlRenderThemeM

private MapView mapView;
private IRenderTheme theme;
private Uri tempMapUri;

public static MapFragment newInstance() {
MapFragment instance = new MapFragment();
Expand All @@ -79,6 +80,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
relativeLayout.addView(mapView);

// Open map
Toast.makeText(getActivity(), "Select a map file", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Expand All @@ -89,22 +91,23 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_MAP_FILE && resultCode == Activity.RESULT_OK) {
if (data != null) {
tempMapUri = data.getData();
Uri uri = data.getData();
loadMap(uri);

// Open map
// Open theme
Toast.makeText(getActivity(), "Map file selected. Now, optionally select a theme.", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, SELECT_THEME_FILE);
}
} else if (requestCode == SELECT_THEME_FILE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
openMap(tempMapUri, uri);
loadTheme(uri);
}
}

// themeUri may be null, using default theme then
private void openMap(Uri mapUri, Uri themeUri) {
private void loadMap(Uri mapUri) {
try {
// Tile source
MapFileTileSource tileSource = new MapFileTileSource();
Expand All @@ -120,15 +123,7 @@ private void openMap(Uri mapUri, Uri themeUri) {
// Label layer
mapView.map().layers().add(new LabelLayer(mapView.map(), tileLayer));

// Render theme
if (themeUri != null) {
final List<String> xmlThemes = ZipXmlThemeResourceProvider.scanXmlThemes(new ZipInputStream(new BufferedInputStream(getActivity().getContentResolver().openInputStream(themeUri))));
if (xmlThemes.isEmpty()) {
return;
}
ThemeFile theme = new ZipRenderTheme(xmlThemes.get(0), new ZipXmlThemeResourceProvider(new ZipInputStream(new BufferedInputStream(getActivity().getContentResolver().openInputStream(themeUri)))));
mapView.map().setTheme(theme);
} else {
if (theme == null) {
theme = mapView.map().setTheme(VtmThemes.DEFAULT);
}

Expand All @@ -139,12 +134,40 @@ private void openMap(Uri mapUri, Uri themeUri) {
mapScaleBarLayer.getRenderer().setOffset(5 * CanvasAdapter.getScale(), 0);
mapView.map().layers().add(mapScaleBarLayer);

// initial position
MapInfo info = tileSource.getMapInfo();
if (!info.boundingBox.contains(mapView.map().getMapPosition().getGeoPoint())) {
MapPosition pos = new MapPosition();
pos.setByBoundingBox(info.boundingBox, Tile.SIZE * 4, Tile.SIZE * 4);
mapView.map().setMapPosition(pos);
}

} catch (Exception e) {
/*
* In case of map file errors avoid crash, but developers should handle these cases!
*/
e.printStackTrace();
}
}

private void loadTheme(Uri themeUri) {
try {
if (theme != null) {
theme.dispose();
}

// Render theme (themeUri may be null, using default theme then)
if (themeUri != null) {
final List<String> xmlThemes = ZipXmlThemeResourceProvider.scanXmlThemes(new ZipInputStream(new BufferedInputStream(getActivity().getContentResolver().openInputStream(themeUri))));
if (xmlThemes.isEmpty()) {
return;
}
ThemeFile themeFile = new ZipRenderTheme(xmlThemes.get(0), new ZipXmlThemeResourceProvider(new ZipInputStream(new BufferedInputStream(getActivity().getContentResolver().openInputStream(themeUri)))));
theme = mapView.map().setTheme(themeFile);
} else {
theme = mapView.map().setTheme(VtmThemes.DEFAULT);
}

} catch (Exception e) {
/*
* In case of map file errors avoid crash, but developers should handle these cases!
Expand Down

0 comments on commit 81b45f3

Please sign in to comment.