Skip to content

Commit

Permalink
bugi fixes & bearing
Browse files Browse the repository at this point in the history
  • Loading branch information
mstahv committed Apr 11, 2024
1 parent 3e62514 commit 168f207
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<dependency>
<groupId>org.parttio</groupId>
<artifactId>vaadin-js-loader</artifactId>
<version>2.2.0</version>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
Expand All @@ -106,7 +106,7 @@
<dependency>
<groupId>in.virit</groupId>
<artifactId>viritin</artifactId>
<version>2.7.3</version>
<version>2.8.9</version>
<scope>test</scope>
</dependency>

Expand Down
21 changes: 16 additions & 5 deletions src/main/java/org/vaadin/addons/maplibre/MapLibre.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public void setCenter(Geometry geom) {
setCenter(geom.getCentroid().getCoordinate());
}

public void setBearing(double bearing) {
js("map.setBearing($bearing);", Map.of("bearing", bearing));
}

private void addSource(String name, Geometry geometry) {
js("""
map.addSource('$name', {
Expand Down Expand Up @@ -455,6 +459,8 @@ public void addMoveEndListener(MoveEndListener listener) {

getElement().addEventListener("map-moveend", domEvent -> {
MoveEndEvent event = new MoveEndEvent(domEvent);
this.zoomLevel = event.getZoomLevel();
this.center = event.getViewPort().center.getCoordinate();
for (MoveEndListener l : moveEndListeners) {
l.onMove(event);
}
Expand Down Expand Up @@ -484,13 +490,18 @@ public void fitToContent() {
idToLayer.values().forEach(layer ->
geometries.add(layer.getGeometry()));
if (geometries.size() > 0) {
Envelope env = geometries.get(0).getEnvelopeInternal();
for (Geometry g : geometries) {
if (g != null) {
env.expandToInclude(g.getEnvelopeInternal());
try {
Envelope env = geometries.get(0).getEnvelopeInternal();
for (Geometry g : geometries) {
if (g != null) {
env.expandToInclude(g.getEnvelopeInternal());
}
}
fitTo(env, 20);
} catch (Exception e) {

e.printStackTrace();
}
fitTo(env, 20);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.vaadin.addons.maplibre;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.vaadin.firitin.components.RichText;
import org.vaadin.firitin.geolocation.Geolocation;
import org.vaadin.firitin.geolocation.GeolocationCoordinates;
import org.vaadin.firitin.geolocation.GeolocationOptions;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

@Route
public class GpsTrackingAndRotation extends VerticalLayout {
static GeometryFactory gf = new GeometryFactory();

private Marker marker;
private Geolocation geolocation;
private List<Coordinate> tailpoints = new ArrayList<>();
private LineLayer tail;

public GpsTrackingAndRotation() {
MapLibre map = new MapLibre("https://api.maptiler.com/maps/streets/style.json?key=G5n7stvZjomhyaVYP0qU");
map.setHeight("400px");
map.setWidth("100%");
map.setCenter(24.945831, 60.192059);
map.setZoomLevel(15);
add(map);

var options = new GeolocationOptions();
options.setEnableHighAccuracy(true);
geolocation = Geolocation.watchPosition(geolocation -> {
var coords = geolocation.getCoords();
Coordinate coordinate = new Coordinate(coords.getLongitude(), coords.getLatitude(), 0);

if (marker == null) {
marker = map.addMarker(coords.getLongitude(), coords.getLatitude());
marker.setColor("#009900");
map.flyTo(marker.getGeometry());
} else {
// update position
var p = gf.createPoint(coordinate);
marker.setPoint(p);

Double heading = geolocation.getCoords().getHeading();
// heading is only available if moving, map will tilt then
if(heading != null) {
map.setBearing(heading);
}
map.getViewPort().thenAccept(viewPort -> {
// adjust if marker has moved out of viewport
if(!viewPort.getBounds().contains(p)) {
map.flyTo(p);
}
});
}
tailpoints.add(coordinate);
if (tailpoints.size() > 100) {
tailpoints.remove(0);
}
if (tailpoints.size() == 2) {
var ls = gf.createLineString(tailpoints.toArray(new Coordinate[0]));
// create and start showing the tail
tail = map.addLineLayer(ls, new LinePaint("#00ff00", 2.0));
} else if (tailpoints.size() > 2) {
// update tail
tail.addCoordinates(tailpoints.size() == 100 ? 1 : 0, tailpoints.get(tailpoints.size() - 1));
}

}, error -> {
Notification.show(error.getErrorMessage());
}, options);

}
}

0 comments on commit 168f207

Please sign in to comment.