Skip to content

Commit

Permalink
Fixes and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
mstahv committed Feb 25, 2024
1 parent d1910eb commit 1e4325e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/vaadin/addons/maplibre/DrawControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public CompletableFuture<GeometryCollection> getAll() {
return v;
}

public void clear() {
js("draw.deleteAll()", Collections.emptyMap());
}

public void setGeometry(Geometry geometry) {
String geojsonstr = new GeoJsonWriter().write(geometry);
js("""
Expand Down
86 changes: 73 additions & 13 deletions src/main/java/org/vaadin/addons/maplibre/MapLibre.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.geojson.GeoJsonWriter;
import org.parttio.vaadinjsloader.JSLoader;
import org.vaadin.addons.velocitycomponent.AbstractVelocityJsComponent;
Expand All @@ -39,6 +41,8 @@
@Tag("div")
public class MapLibre extends AbstractVelocityJsComponent implements HasSize, HasStyle {

static GeometryFactory gf = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326);

private final HashMap<String, Layer> idToLayer = new HashMap<>();
private ArrayList<MoveEndListener> moveEndListeners;
private Coordinate center = new Coordinate(0, 0);
Expand Down Expand Up @@ -215,26 +219,44 @@ protected VelocityContext getVelocityContext() {
}

public void setCenter(double x, double y) {
this.center = new Coordinate(x, y);
setCenter(new Coordinate(x, y));
}

public void setCenter(Coordinate coordinate) {
this.center = coordinate;
js("map.setCenter($GeoJsonHelper.toJs($this.center));");
}

public void setCenter(Geometry geom) {
setCenter(geom.getCentroid().getCoordinate());
}

public void fitTo(Geometry geom, double padding) {
Envelope envelope = geom.getEnvelopeInternal();
envelope.expandBy(padding);
js("""
fitTo(envelope, padding);
}
protected void fitTo(Envelope envelope, double padding) {
fitTo("""
const bounds = new maplibregl.LngLatBounds(
[%s, %s], [%s, %s]);;
map.fitBounds(bounds);
""".formatted(envelope.getMinX(), envelope.getMinY(), envelope.getMaxX(), envelope.getMaxY()));
map.fitBounds(bounds, {padding: %s});
""".formatted(envelope.getMinX(), envelope.getMinY(), envelope.getMaxX(), envelope.getMaxY(), padding));
}

public void flyTo(double x, double y, double zoom) {
private void fitTo(String envelope) {
js(envelope);
}

public void flyTo(double x, double y, Double zoom) {
js("""
map.flyTo({
center: [%s, %s],
zoom: %s
});
const opts = {
center: [%s, %s]
}
const z = %s;
if(z != null) {
opts.zoom = z;
}
map.flyTo(opts);
""".formatted(x, y, zoom));
}

Expand Down Expand Up @@ -264,9 +286,14 @@ protected void js(String js) {
js(js, Collections.emptyMap());
}

public void flyTo(Geometry geometry, int i) {
public void flyTo(Geometry geometry, double zoomLevel) {
Point centroid = geometry.getCentroid();
flyTo(centroid.getX(), centroid.getY(), zoomLevel);
}

public void flyTo(Geometry geometry) {
Point centroid = geometry.getCentroid();
flyTo(centroid.getX(), centroid.getY(), i);
flyTo(centroid.getX(), centroid.getY(), null);
}

String registerJsCallback(Runnable r) {
Expand Down Expand Up @@ -367,6 +394,19 @@ public CompletableFuture<ViewPort> getViewPort() {
return res;
}

public void fitToContent() {
List<Geometry> geometries = new ArrayList<>();
idToLayer.values().forEach(layer ->
geometries.add(layer.getGeometry()));
if(geometries.size() > 0) {
Envelope env = geometries.get(0).getEnvelopeInternal();
for(Geometry g : geometries) {
env.expandToInclude(g.getEnvelopeInternal());
}
fitTo(env, 20);
}
}

public interface MoveEndListener {
void onMove(MoveEndEvent event);
}
Expand All @@ -379,8 +419,22 @@ public interface MapClickListener {

public record ViewPort(Point southWest, Point northEast, Point center, double bearing, double pitch) {


public Polygon getBounds() {
// 4326
// TODO this will most likely now not be perfect if bearing/pitch
// is used, would need some math...
Coordinate[] shell = new Coordinate[5];
shell[0] = southWest.getCoordinate();
shell[4] = southWest.getCoordinate();
shell[2] = northEast.getCoordinate();
shell[1] = new Coordinate(southWest.getX(), northEast.getY());
shell[3] = new Coordinate(northEast.getX(), southWest.getY());
return gf.createPolygon(shell);

}

public static ViewPort of(JsonObject o) {
GeometryFactory gf = new GeometryFactory();
var southWest = gf.createPoint(new Coordinate(
o.getObject("sw").getNumber("lng"),
o.getObject("sw").getNumber("lat")));
Expand All @@ -391,6 +445,7 @@ public static ViewPort of(JsonObject o) {
double clng = o.getObject("c").getNumber("lng");
return new ViewPort(southWest, northEast, gf.createPoint(new Coordinate(clng, clat)), o.getNumber("bearing"), o.getNumber("pitch"));
}

}

public class MoveEndEvent {
Expand Down Expand Up @@ -448,4 +503,9 @@ record PointRecord(double x, double y) {
}
}

public void removeAll() {
ArrayList<Layer> layers = new ArrayList<>(this.idToLayer.values());
layers.forEach(l -> removeLayer(l));
}

}
9 changes: 8 additions & 1 deletion src/main/java/org/vaadin/addons/maplibre/Marker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ public class Marker extends Layer {
super(map, id,new GeometryFactory().createPoint(coordinate));
}

public void withPopup(String html) {
public Marker withPopup(String html) {
map.js("""
const marker = component.markers['$id'];
const popup = new maplibregl.Popup({closeButton: true, closeOnClick: true})
.setLngLat(marker.getLngLat())
.setHTML('$html');
marker.setPopup(popup);
""", Map.of("id", id, "html", html));
return this;
}

public void setPoint(Point point) {
Expand All @@ -32,6 +33,12 @@ public void setPoint(Point point) {
""", Map.of("id", id, "x", point.getX(), "y", point.getY()));
}

public void openPopup() {
map.js("""
const marker = component.markers['$id'];
marker.getPopup().addTo(map);
""", Map.of("id", id));
}

public interface DragEndListener {
void dragEnd(Coordinate coordinate);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/vaadin/addons/maplibre/PointField.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public PointField withStyleUrl(String styleUrl) {
}

private void assingPointFromCoordinate(Coordinate coordinate) {
point = new GeometryFactory().createPoint(coordinate);
point = MapLibre.gf.createPoint(coordinate);
}

@Override
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/org/vaadin/addons/maplibre/OsmViaMapTiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ public OsmViaMapTiler() {
MapLibre map = new MapLibre(new URI("https://api.maptiler.com/maps/streets/style.json?key=G5n7stvZjomhyaVYP0qU"));
map.setHeight("400px");
map.setWidth("100%");
map.addMarker(22.300, 60.452).withPopup("Hello from Vaadin!");
map.addMarker(22.300, 60.452)
.withPopup("Hello from Vaadin!")
.openPopup();


Polygon polygon = (Polygon) new WKTReader().read("POLYGON((22.290 60.428, 22.310 60.429, 22.31 60.47, 22.28 60.47, 22.290 60.428))");

map.addFillLayer(polygon, new FillPaint("red", 0.2));

map.setCenter(22.300, 60.452);
map.setZoomLevel(13);
map.fitToContent();

map.addMapClickListener(e -> {
String str = "Clicked on Map: " + e.getPoint().toString();
Expand All @@ -48,7 +50,8 @@ public OsmViaMapTiler() {
});

map.addMoveEndListener(event -> {
Notification.show(event.toString()).setPosition(Notification.Position.TOP_END);
Notification notification = Notification.show(event.toString());
notification.setPosition(Notification.Position.TOP_END);
});

addAndExpand(map);
Expand All @@ -64,7 +67,7 @@ public OsmViaMapTiler() {

Button seeWorld = new Button("See the world (flyTo(0,0,0)");
seeWorld.addClickListener(e -> {
map.flyTo(0, 0, 0);
map.flyTo(0, 0, 0.0);
});
Button plotYourself = new Button("Plot yourself");
plotYourself.addClickListener(e -> {
Expand All @@ -85,7 +88,7 @@ public OsmViaMapTiler() {

Button showExtent = new VButton("Detect viewport", e-> {
map.getViewPort().thenAccept(vp -> {
Notification.show(vp.toString(), 3000, Notification.Position.TOP_END);
Notification.show(vp.toString() + vp.getBounds(), 3000, Notification.Position.TOP_END);
});
});

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/vaadin/addons/maplibre/it/MopoSmokeIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ public void osmViaMapTiler() {

mopo.click(page.getByText("Plot yourself"));

// Wait for notifications to close
// TODO Vaadin should have API for it
// TODO Mopo should have API to wait for it
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

page.getByLabel("Map marker").last().click();

assertThat(page.locator("vaadin-notification-card").first())
Expand Down

0 comments on commit 1e4325e

Please sign in to comment.