Furhter work on MapStyle

This commit is contained in:
2026-04-16 17:42:25 +02:00
parent 95a454bb6d
commit 63d41d14d1
7 changed files with 86 additions and 69 deletions
@@ -5,6 +5,7 @@ import eu.konggdev.strikemaps.Component;
import eu.konggdev.strikemaps.factory.AlertDialogFactory;
import eu.konggdev.strikemaps.helper.UserPrefsHelper;
import eu.konggdev.strikemaps.map.renderer.implementation.VtmRenderer;
import eu.konggdev.strikemaps.map.style.MapStyle;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.geojson.Feature;
@@ -18,7 +19,7 @@ public class MapComponent implements Component {
MapRenderer mapRenderer;
AppController app;
public String style;
public MapStyle style;
public Map<Class<? extends MapOverlay>, MapOverlay> overlays = new HashMap<>();
public MapComponent(AppController ref) {
this.app = ref;
@@ -37,7 +38,7 @@ public class MapComponent implements Component {
return new FragmentLayoutContentMap(mapRenderer.getView());
}
public void setStyle(String style) {
public void setStyle(MapStyle style) {
this.style = style;
mapRenderer.reload();
}
@@ -0,0 +1,16 @@
package eu.konggdev.strikemaps.map.layer;
import org.maplibre.android.style.layers.Layer;
import org.maplibre.android.style.sources.GeoJsonSource;
//FIXME: Get rid of reliance on MapLibr
//Most likely implement an "AdditionalMapLayer" or something of that sorts (?)
public class MapLayer {
public GeoJsonSource source;
public Layer layer;
public MapLayer(GeoJsonSource source, Layer layer) {
this.source = source;
this.layer = layer;
}
}
@@ -4,11 +4,14 @@ import android.view.View;
import androidx.annotation.NonNull;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.konggdev.strikemaps.helper.FileHelper;
import eu.konggdev.strikemaps.helper.UserPrefsHelper;
import eu.konggdev.strikemaps.map.overlay.MapOverlay;
import eu.konggdev.strikemaps.map.layer.MapLayer;
import eu.konggdev.strikemaps.map.renderer.MapRenderer;
import eu.konggdev.strikemaps.map.style.MapStyle;
import org.maplibre.android.MapLibre;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.android.maps.MapLibreMap;
@@ -44,11 +47,20 @@ public class MapLibreNativeRenderer implements MapRenderer, OnMapReadyCallback {
@Override
public void reload() {
map.setStyle(new Style.Builder().fromJson(controller.style), style -> {
for(MapOverlay overlay : controller.overlays.values()) {
passLayer(overlay.makeLayer());
}
});
ObjectMapper mapper = new ObjectMapper();
MapStyle style = controller.style;
try {
ObjectNode root = style.metadata.deepCopy();
root.set("sources", mapper.valueToTree(style.sources));
root.set("layers", style.layerDefinitions);
map.setStyle(new Style.Builder().fromJson(mapper.writeValueAsString(root)), intStyle -> {
for(MapOverlay overlay : controller.overlays.values()) {
passLayer(overlay.makeLayer());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
@@ -65,7 +77,7 @@ public class MapLibreNativeRenderer implements MapRenderer, OnMapReadyCallback {
public void onMapReady(@NonNull MapLibreMap maplibreMap) {
this.map = maplibreMap;
controller.style = FileHelper.loadStringFromAssetFile(UserPrefsHelper.startupMapStyle(app.getPrefs()), app);
controller.setStyle(MapStyle.fromMapLibreJsonFile(UserPrefsHelper.startupMapStyle(app.getPrefs()), app));
//I have my own implementation of attribution that credits MapLibre among others, it's not as bad as it looks :)
map.getUiSettings().setLogoEnabled(false);
@@ -73,6 +85,5 @@ public class MapLibreNativeRenderer implements MapRenderer, OnMapReadyCallback {
map.addOnMapClickListener(point -> controller.onMapClick(point));
map.addOnMapLongClickListener(point -> controller.onMapLongClick(point));
this.reload();
}
}
@@ -1,12 +1,9 @@
package eu.konggdev.strikemaps.map.source;
public class MapSource {
public final String url;
public final String type;
public final String schema;
public MapSource(String url, String type, String schema) {
this.url = url;
this.type = type;
this.schema = schema;
}
public String url;
public String type;
public String schema;
public MapSource() { }
}
@@ -1,25 +1,27 @@
package eu.konggdev.strikemaps.map.style;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.helper.FileHelper;
import eu.konggdev.strikemaps.map.layer.MapLayer;
import eu.konggdev.strikemaps.map.source.MapSource;
import org.json.JSONObject;
import java.util.*;
public class MapStyle {
public int version;
//Only local data
public String name;
public String icon;
public String description;
public Bitmap icon;
public JsonNode metadata; // everything except layers + sources
public Map<String, MapSource> sources;
public List<MapLayer> layers;
public JsonNode raw;
public JsonNode layerDefinitions; // the "layers" array
//FIXME
public static MapStyle fromMapLibreJsonFile(String filename, AppController app) {
@@ -30,34 +32,38 @@ public class MapStyle {
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(styleContents);
MapStyle style = new MapStyle();
style.version = root.path("version").asInt();
MapStyle style = new MapStyle();
style.name = root.path("name").asText();
style.icon = root.path("icon").asText();
style.description = root.path("description").asText();
style.icon = getIcon(root.path("icon").asText(), app);
style.sources = mapper.convertValue(
root.path("sources"),
new TypeReference<Map<String, MapSource>>() {}
);
style.layers = new ArrayList<>();
for (JsonNode layerNode : root.path("layers")) {
style.layerDefinitions = root.path("layers");
MapLayer layer = mapper.treeToValue(layerNode, MapLayer.class);
layer.raw = layerNode; // IMPORTANT
style.layers.add(layer);
}
style.raw = root; // full backup
ObjectNode metadata = root.deepCopy();
metadata.remove("layers");
metadata.remove("sources");
style.metadata = metadata;
return style;
} catch ( Exception e ) {
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Bitmap getIcon(String iconLocator, AppController app) {
switch(iconLocator.split("//")[0]) {
//TODO: https
case "assets:":
return BitmapFactory.decodeStream(FileHelper.openAssetStream("bundled/icon/" + iconLocator.split("//")[1], app));
default:
app.logcat("Unimplemented icon locator space: " + iconLocator);
return null;
}
}
}