Android example for render theme styles #93, closes #255

This commit is contained in:
Emux 2016-12-01 21:08:12 +02:00
parent 38a9c71bc2
commit c21d7b4390
9 changed files with 173 additions and 21 deletions

View File

@ -56,6 +56,9 @@
<activity
android:name=".MapsforgeMapActivity$MapFilePicker"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".MapsforgeStyleActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".MarkerOverlayActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<rendertheme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" map-background="#fffcfa"
version="1" xmlns="http://opensciencemap.org/rendertheme"
xsi:schemaLocation="http://opensciencemap.org/rendertheme https://raw.githubusercontent.com/mapsforge/vtm/master/resources/rendertheme.xsd">
<stylemenu defaultlang="en" defaultvalue="1" id="menu">
<layer enabled="true" id="nature">
<name lang="de" value="Natur" />
<name lang="en" value="Nature" />
<name lang="es" value="Naturaleza" />
<name lang="fr" value="Nature" />
<cat id="nature" />
</layer>
<layer id="base">
<cat id="sea" />
<cat id="land" />
</layer>
<!-- Show nature layers (enabled by default) -->
<layer id="1" parent="base" visible="true">
<name lang="de" value="1" />
<name lang="en" value="1" />
<name lang="es" value="1" />
<name lang="fr" value="1" />
<overlay id="nature" />
</layer>
<!-- Don't show nature layers -->
<layer id="2" parent="base" visible="true">
<name lang="de" value="2" />
<name lang="en" value="2" />
<name lang="es" value="2" />
<name lang="fr" value="2" />
</layer>
</stylemenu>
<m cat="sea" e="way" k="natural" v="issea|sea">
<area fill="#b5d6f1" mesh="true" />
</m>
<m cat="land" e="way" k="natural" v="nosea">
<area fill="#f8f8f8" mesh="true" />
</m>
<m cat="nature" e="way" k="natural|landuse" v="forest|wood">
<area fill="#83aa5b" />
</m>
</rendertheme>

View File

@ -1,7 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_settings"
android:title="@string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/styles"
android:checkableBehavior="single">
<item
android:id="@+id/style_1"
android:checked="true"
android:title="@string/style_1" />
<item
android:id="@+id/style_2"
android:title="@string/style_2" />
</group>
</menu>

View File

@ -7,26 +7,21 @@
<item
android:id="@+id/theme_default"
android:checked="true"
android:showAsAction="never"
android:title="@string/theme_default"></item>
android:title="@string/theme_default" />
<item
android:id="@+id/theme_tubes"
android:showAsAction="never"
android:title="@string/theme_tubes"></item>
android:title="@string/theme_tubes" />
<item
android:id="@+id/theme_osmarender"
android:showAsAction="never"
android:title="@string/theme_osmarender"></item>
android:title="@string/theme_osmarender" />
<item
android:id="@+id/theme_newtron"
android:showAsAction="never"
android:title="@string/theme_newtron"></item>
android:title="@string/theme_newtron" />
</group>
<item
android:id="@+id/gridlayer"
android:showAsAction="never"
android:checkable="true"
android:title="@string/menu_gridlayer"></item>
android:title="@string/menu_gridlayer" />
</menu>
</menu>

View File

@ -1,7 +1,6 @@
<resources>
<string name="app_name">VTM Samples</string>
<string name="menu_settings">Settings</string>
<string name="theme_default">Default</string>
<string name="theme_tubes">Tubes</string>
<string name="theme_osmarender">Osmarender</string>
@ -14,6 +13,8 @@
<string name="styler_mode_area">Area</string>
<string name="styler_mode_outline">Outline</string>
<string name="styler_controls_toggle">Controls</string>
<string name="menu_gridlayer">Gridlayer</string>
<string name="style_1">Show nature</string>
<string name="style_2">Hide nature</string>
<string name="menu_gridlayer">Grid</string>
</resources>

View File

@ -118,7 +118,7 @@ public class MapsforgeMapActivity extends MapActivity {
if (tileSource.setMapFile(file)) {
VectorTileLayer l = mMap.setBaseMap(tileSource);
mMap.setTheme(VtmThemes.DEFAULT);
loadTheme(null);
mMap.layers().add(new BuildingLayer(mMap, l));
mMap.layers().add(new LabelLayer(mMap, l));
@ -132,4 +132,8 @@ public class MapsforgeMapActivity extends MapActivity {
}
}
}
protected void loadTheme(final String styleId) {
mMap.setTheme(VtmThemes.DEFAULT);
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 2016 devemux86
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.test;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import org.oscim.android.theme.AssetsRenderTheme;
import org.oscim.theme.XmlRenderThemeMenuCallback;
import org.oscim.theme.XmlRenderThemeStyleLayer;
import org.oscim.theme.XmlRenderThemeStyleMenu;
import java.util.Set;
/**
* Load render theme from Android assets folder and show a configuration menu based on stylemenu.
*/
public class MapsforgeStyleActivity extends MapsforgeMapActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.style_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.style_1:
item.setChecked(true);
loadTheme("1");
mMap.clearMap();
Toast.makeText(this, "Show nature layers", Toast.LENGTH_SHORT).show();
return true;
case R.id.style_2:
item.setChecked(true);
loadTheme("2");
mMap.clearMap();
Toast.makeText(this, "Hide nature layers", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
@Override
protected void loadTheme(final String styleId) {
mMap.setTheme(new AssetsRenderTheme(this, "", "vtm/stylemenu.xml", new XmlRenderThemeMenuCallback() {
@Override
public Set<String> getCategories(XmlRenderThemeStyleMenu renderThemeStyleMenu) {
// Use the selected style or the default
String style = styleId != null ? styleId : renderThemeStyleMenu.getDefaultValue();
// Retrieve the layer from the style id
XmlRenderThemeStyleLayer renderThemeStyleLayer = renderThemeStyleMenu.getLayer(style);
if (renderThemeStyleLayer == null) {
System.err.println("Invalid style " + style);
return null;
}
// First get the selected layer's categories that are enabled together
Set<String> categories = renderThemeStyleLayer.getCategories();
// Then add the selected layer's overlays that are enabled individually
// Here we use the style menu, but users can use their own preferences
for (XmlRenderThemeStyleLayer overlay : renderThemeStyleLayer.getOverlays()) {
if (overlay.isEnabled())
categories.addAll(overlay.getCategories());
}
// This is the whole categories set to be enabled
return categories;
}
}));
}
}

View File

@ -41,6 +41,7 @@ public class Samples extends Activity {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.samples);
linearLayout.addView(createButton(SimpleMapActivity.class));
linearLayout.addView(createButton(MapsforgeMapActivity.class));
linearLayout.addView(createButton(MapsforgeStyleActivity.class));
linearLayout.addView(createButton(MapboxMapActivity.class));
linearLayout.addView(createButton(OsmJsonMapActivity.class));