Class HikingEditor

HikingEditor

The HikingEditor class is responsible for editing hiking tracks and stopovers.

To construct a new HikingEditor, you need to pass an OpenLayers map instance:

const hikingEditor = new sww.HikingEditor({
map: map,
type: "hiking_proposition",
});

This will allow the user to create a new hiking proposition.

To edit an existing hiking activity, you need to pass the uuid of the hiking and the type:

const hikingEditor = new sww.HikingEditor({
map: map,
type: "hiking_activity",
uuid: "12345678-1234-1234-1234-123456789012",
});

When the hiking is loaded, a loaded event is fired. The event detail contains the same properties as the change event.

The constructor takes an option object HikingEditor.HikingEditorOptions with the following properties:

  • map: the OpenLayers map instance
  • type: the type of the hiking, one of hiking, hiking_activity or hiking_proposition
  • uuid: the optional uuid of the hiking to edit

The type property can be changed with the HikingEditor.setType function:

hikingEditor.setType("hiking");

An other hiking can be loaded with the HikingEditor.loadHiking function:

hikingEditor.loadHiking("12345678-1234-1234-1234-123456789012");

To save the hiking, call the HikingEditor.save function, this will create or update the hiking on the server.

To delete the hiking, call the HikingEditor.delete function, this will delete the hiking from the server.

To update the properties of the hiking that are not related to the geometries (for example the title, description, or any other property), call the HikingEditor.updateHikingProperties function and pass an object with the properties to update:

hikingEditor.updateHikingProperties({
title: {"fr": "title fr", "de": "title de", "it": "title it", "en": "title en"},
best_hiking_time: [1, 2],
});

FIXME: what if the the hiking is not saved yet ?

  • A new control point is added with a single click on the map.
  • A new control point is added between on a line by dragging the line.
  • The control points and stopovers can be moved by dragging.
  • The control points and stopovers can be removed by double clicking on them.
  • A new stopover is created by clicking on a control point.

After each user interaction, a change event is fired.

By default, the editor is in edit mode. In this mode, the user can add, move and remove control points. To switch to view mode, set the edit property to false:

hikingEditor.edit = false;

The lines are automatically snapped to the closest hiking path. To disable snapping, set the snapping property to false:

hikingEditor.snapping = false;

To reverse the track, call the HikingEditor.reverse function:

hikingEditor.reverse();

The reverse function will trigger a change event.

The editor can undo or redo the actions to the geometries of the control points. To undo the last action, call the HikingEditor.undo function:

hikingEditor.undo();

To redo the last action, call the HikingEditor.redo function:

hikingEditor.redo();
  • canUndo: true if the user can undo the last action
  • canRedo: true if the user can redo the last action

The undo and redo functions will trigger a change event.

The control points are the points that are between the segments of the track, they can be added, moved and removed by the user. They are represented by a small circle on the map.

The stopovers are a special type of control points that the user chooses to create by clicking on a control point. The first and last control points are always stopovers. They are represented by a big circle on the map.

A stopover has a name and and an optional list of features. A duration and a distance from the previous stopover is computed for each stopover.

Every time a change is made to the stopovers, a change event is fired. The event detail contains the values of the stopovers.

To register an event listener for the change event, use the addEventListener method:

hikingEditor.addEventListener("change", (event) => {
for (const stopover of event.detail.stopovers) {
console.log(stopover);
}
});

All the stopovers are returned in the stopovers property of the event detail, each stopover has the following properties:

  • uid: the unique id of the stopover. this id is used to identify the stopover in the other methods.
  • swissNames: an array of swiss names for this location
  • name: the name of the stopover, if the name was not set by the HikingEditor.setStopOverName function, the first swiss name is used.
  • features: an array of features that the user can add to the stopover
  • duration: the walking time duration of the stopover from the previous stopover in minutes. This value can be overwritten by using the HikingEditor.setStopOverDuration function.
  • distance: the distance of the stopover from the previous stopover in meters
  • coordinates: the coordinates of the stopover in EPSG:4326 projection

In addition to the stopovers, the event detail contains the following properties:

  • type: the type of the hiking, one of hiking, hiking_activity or hiking_proposition
  • canUndo: true if the user can undo the last action
  • canRedo: true if the user can redo the last action

To change the name of a stopover, use the HikingEditor.setStopOverName function and pass the uid of the stopover:

hikingEditor.setStopOverName(123, "Bümpliz Nord Bahnhof");

Calling this method will trigger a change event.

Every stopover can have a list of features representing a point of interest at this location. A feature is an object with a type property and an optional title property.

To set the features of a stopover, use the HikingEditor.setStopOverFeatures function and pass the uid of the stopover:

hikingEditor.setStopOverFeatures(123, [
{ type: "shopping", title: "Food and Stuff" },
{ type: "parking" },
]);

When calling this function, existing features are replaced by the new list.

Calling this method will trigger a change event.

To set a custom duration of a stopover in minutes, use the HikingEditor.setStopOverDuration function and pass the uid of the stopover:

hikingEditor.setStopOverDuration(123, 30);

Calling this method will trigger a change event.

To remove a stopover, use the HikingEditor.removeStopOver function and pass the uid of the stopover:

hikingEditor.removeStopOver(123);

When a stopover is removed, the custom name and features are also removed and cannot be recovered. Calling this method will trigger a change event.

To create a hiking from a GPX or a KML file, use the HikingEditor.importGpxOrKml function and pass the file. For GPX, the line coordinates will be primarily extracted from tracks and if no tracks are found, from routes. Control points for both formats are snapped to the line - meaning that every control point has a corresponding point on the line. The first and last control points are always created as stopovers, even if no waypoints are found. The coordinates are expected to be in EPSG:4326 projection and in Cartesian order (lon, lat).

Hierarchy

  • EventTarget
    • HikingEditor

Constructors

  • Parameters

    • options: HikingEditorOptions

    Returns HikingEditor

Accessors

  • get edit(): boolean
  • Returns boolean

  • set edit(value): void
  • Parameters

    • value: boolean

    Returns void

  • get snapping(): boolean
  • Returns boolean

  • set snapping(value): void
  • Parameters

    • value: boolean

    Returns void

Methods

  • Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

    The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

    When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

    When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

    When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

    If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

    The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.

    MDN Reference

    Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optionaloptions: boolean | AddEventListenerOptions

    Returns void

  • Computes the properties (statistics) of a track, including total time, distance, ascent, descent, physical demand, time and distance for each stopover.

    Returns TrackProperties

  • Delete the hiking from the server

    Returns void

  • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

    MDN Reference

    Parameters

    • event: Event

    Returns boolean

  • Returns HikingProfile

  • Returns the uuid of the hiking. If the hiking is not saved yet, this function returns undefined.

    Returns undefined | string

  • Handle import of GPX and KML files

    Parameters

    • file: File

    Returns void

  • Load the hiking from the server.

    Parameters

    • uuid: undefined | string

    Returns Promise<void>

  • Redo the last action

    Returns void

  • Removes the event listener in target's event listener list with the same type, callback, and options.

    MDN Reference

    Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optionaloptions: boolean | EventListenerOptions

    Returns void

  • Remove a stopover from the stopover list

    Parameters

    • uid: string

    Returns void

  • Reverse the track

    Returns void

  • Create or update the hiking to the server

    Returns Promise<string>

  • Set the duration of a stopover in minutes

    Parameters

    • uid: string
    • duration: string | number

    Returns void

  • Set the features of a stopover

    Parameters

    • uid: string
    • features: unknown[]

    Returns void

  • Change the name of a stopover

    Parameters

    • uid: string
    • name: string

    Returns void

  • Set the type of the hiking. The type can be one of hiking, hiking_activity or hiking_proposition.

    Parameters

    • type: "hiking" | "hiking_activity" | "hiking_proposition"

    Returns void

  • Undo the last action

    Returns void

  • Update the hiking properties

    Parameters

    • properties: Record<string, unknown>

    Returns void