mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-09-02 16:52:31 +00:00
basic routing working
This commit is contained in:
@@ -13,6 +13,7 @@ export class RoutingControls {
|
|||||||
|
|
||||||
toggleMarkersForZoomLevelAndBoundsBinded: () => void = this.toggleMarkersForZoomLevelAndBounds.bind(this);
|
toggleMarkersForZoomLevelAndBoundsBinded: () => void = this.toggleMarkersForZoomLevelAndBounds.bind(this);
|
||||||
extendFileBinded: (e: mapboxgl.MapMouseEvent) => void = this.extendFile.bind(this);
|
extendFileBinded: (e: mapboxgl.MapMouseEvent) => void = this.extendFile.bind(this);
|
||||||
|
busy: boolean = false;
|
||||||
|
|
||||||
constructor(map: mapboxgl.Map, file: Writable<GPXFile>) {
|
constructor(map: mapboxgl.Map, file: Writable<GPXFile>) {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
@@ -42,9 +43,8 @@ export class RoutingControls {
|
|||||||
let anchor = anchors[i];
|
let anchor = anchors[i];
|
||||||
if (anchor.point._data.index >= segment.trkpt.length || anchor.point !== segment.trkpt[anchor.point._data.index]) { // Point removed
|
if (anchor.point._data.index >= segment.trkpt.length || anchor.point !== segment.trkpt[anchor.point._data.index]) { // Point removed
|
||||||
anchors.splice(i, 1);
|
anchors.splice(i, 1);
|
||||||
let markerIndex = this.markers.findIndex(marker => marker._simplified === anchor);
|
this.markers[i].remove();
|
||||||
this.markers[markerIndex].remove();
|
this.markers.splice(i, 1);
|
||||||
this.markers.splice(markerIndex, 1);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
@@ -93,23 +93,24 @@ export class RoutingControls {
|
|||||||
toggleMarkersForZoomLevelAndBounds() {
|
toggleMarkersForZoomLevelAndBounds() {
|
||||||
let zoom = this.map.getZoom();
|
let zoom = this.map.getZoom();
|
||||||
this.markers.forEach((marker) => {
|
this.markers.forEach((marker) => {
|
||||||
if (marker._simplified.zoom <= zoom && this.map.getBounds().contains(marker.getLngLat())) {
|
Object.defineProperty(marker, '_inZoom', {
|
||||||
|
value: marker._simplified.zoom <= zoom,
|
||||||
|
writable: true
|
||||||
|
});
|
||||||
|
if (marker._inZoom && this.map.getBounds().contains(marker.getLngLat())) {
|
||||||
marker.addTo(this.map);
|
marker.addTo(this.map);
|
||||||
Object.defineProperty(marker, '_inZoom', {
|
|
||||||
value: true,
|
|
||||||
writable: true
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
marker.remove();
|
marker.remove();
|
||||||
Object.defineProperty(marker, '_inZoom', {
|
|
||||||
value: false,
|
|
||||||
writable: true
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateAnchor(e: any) {
|
async updateAnchor(e: any) {
|
||||||
|
if (this.busy) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.busy = true;
|
||||||
|
|
||||||
let marker = e.target;
|
let marker = e.target;
|
||||||
let anchor = marker._simplified;
|
let anchor = marker._simplified;
|
||||||
|
|
||||||
@@ -126,11 +127,15 @@ export class RoutingControls {
|
|||||||
let nextAnchor: SimplifiedTrackPoint | null = null;
|
let nextAnchor: SimplifiedTrackPoint | null = null;
|
||||||
|
|
||||||
for (let i = 0; i < anchors.length; i++) {
|
for (let i = 0; i < anchors.length; i++) {
|
||||||
if (anchors[i].point._data.index < anchor.point._data.index && anchors[i].marker._inZoom) {
|
if (anchors[i].point._data.index < anchor.point._data.index &&
|
||||||
|
anchors[i].point._data.segment === anchor.point._data.segment &&
|
||||||
|
anchors[i].marker._inZoom) {
|
||||||
if (!previousAnchor || anchors[i].point._data.index > previousAnchor.point._data.index) {
|
if (!previousAnchor || anchors[i].point._data.index > previousAnchor.point._data.index) {
|
||||||
previousAnchor = anchors[i];
|
previousAnchor = anchors[i];
|
||||||
}
|
}
|
||||||
} else if (anchors[i].point._data.index > anchor.point._data.index && anchors[i].marker._inZoom) {
|
} else if (anchors[i].point._data.index > anchor.point._data.index &&
|
||||||
|
anchors[i].point._data.segment === anchor.point._data.segment &&
|
||||||
|
anchors[i].marker._inZoom) {
|
||||||
if (!nextAnchor || anchors[i].point._data.index < nextAnchor.point._data.index) {
|
if (!nextAnchor || anchors[i].point._data.index < nextAnchor.point._data.index) {
|
||||||
nextAnchor = anchors[i];
|
nextAnchor = anchors[i];
|
||||||
}
|
}
|
||||||
@@ -150,48 +155,54 @@ export class RoutingControls {
|
|||||||
let end = nextAnchor ? nextAnchor.point._data.index - 1 : anchor.point._data.index;
|
let end = nextAnchor ? nextAnchor.point._data.index - 1 : anchor.point._data.index;
|
||||||
|
|
||||||
if (routeCoordinates.length === 1) {
|
if (routeCoordinates.length === 1) {
|
||||||
return;
|
anchor.point.setCoordinates(coordinates);
|
||||||
} else {
|
} else {
|
||||||
route(routeCoordinates).then((response) => {
|
let response = await route(routeCoordinates);
|
||||||
if (previousAnchor) {
|
if (previousAnchor !== null) {
|
||||||
previousAnchor.zoom = 0;
|
previousAnchor.zoom = 0;
|
||||||
} else {
|
} else {
|
||||||
anchor.zoom = 0;
|
anchor.zoom = 0;
|
||||||
anchor.point = response[0];
|
anchor.point = response[0];
|
||||||
}
|
}
|
||||||
if (nextAnchor) {
|
if (nextAnchor !== null) {
|
||||||
nextAnchor.zoom = 0;
|
nextAnchor.zoom = 0;
|
||||||
} else {
|
} else {
|
||||||
anchor.zoom = 0;
|
anchor.zoom = 0;
|
||||||
anchor.point = response[response.length - 1];
|
anchor.point = response[response.length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
// find closest point to the dragged marker
|
// find closest point to the dragged marker
|
||||||
// and transfer the marker to that point
|
// and transfer the marker to that point
|
||||||
if (previousAnchor && nextAnchor) {
|
if (previousAnchor && nextAnchor) {
|
||||||
let minDistance = Number.MAX_VALUE;
|
let minDistance = Number.MAX_VALUE;
|
||||||
let minIndex = 0;
|
let minIndex = 0;
|
||||||
for (let i = 1; i < response.length - 1; i++) {
|
for (let i = 1; i < response.length - 1; i++) {
|
||||||
let dist = distance(response[i].getCoordinates(), coordinates);
|
let dist = distance(response[i].getCoordinates(), coordinates);
|
||||||
if (dist < minDistance) {
|
if (dist < minDistance) {
|
||||||
minDistance = dist;
|
minDistance = dist;
|
||||||
minIndex = i;
|
minIndex = i;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
anchor.zoom = 0;
|
|
||||||
anchor.point = response[minIndex];
|
|
||||||
}
|
}
|
||||||
|
anchor.zoom = 0;
|
||||||
|
anchor.point = response[minIndex];
|
||||||
|
}
|
||||||
|
|
||||||
marker.setLngLat(anchor.point.getCoordinates());
|
marker.setLngLat(anchor.point.getCoordinates());
|
||||||
|
|
||||||
applyToFileElement(this.file, segment, (segment) => {
|
applyToFileElement(this.file, segment, (segment) => {
|
||||||
segment.replace(start, end, response);
|
segment.replace(start, end, response);
|
||||||
}, true);
|
}, true);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.busy = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async extendFile(e: mapboxgl.MapMouseEvent) {
|
async extendFile(e: mapboxgl.MapMouseEvent) {
|
||||||
|
if (this.busy) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.busy = true;
|
||||||
|
|
||||||
let segments = get(this.file).getSegments();
|
let segments = get(this.file).getSegments();
|
||||||
if (segments.length === 0) {
|
if (segments.length === 0) {
|
||||||
return;
|
return;
|
||||||
@@ -216,5 +227,7 @@ export class RoutingControls {
|
|||||||
this.createMarker(anchor);
|
this.createMarker(anchor);
|
||||||
|
|
||||||
applyToFileStore(this.file, (f) => f.append(response), true);
|
applyToFileStore(this.file, (f) => f.append(response), true);
|
||||||
|
|
||||||
|
this.busy = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user