Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Marker #25

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions src/js/map/add_point.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const markers = [];

/** MaplibreJS source of the GEOJSON data.
*
* @type{import("maplibre-gl/dist/maplibre-gl.js").GeoJSONSource} */
* @type{import("maplibre-gl").GeoJSONSource} */
export let source = undefined;

map.once("load", async () => {
Expand Down Expand Up @@ -165,11 +165,42 @@ function source_loaded() {

// Adding marker
logging.info("Adding new marker");
add_marker(location);
const marker = create_marker(location);
markers.push(marker);
});
}

/** Redraws all the markers on the map.
/**
* Creates a new marker for the map.
*
* @param {import("maplibre-gl").LngLatLike} location - The location of the marker.
* @returns {import("maplibre-gl").Marker} The created marker.
*
* @example
* ```javascript
* const location = [37.7749, -122.4194];
* const marker = create_marker(location);
* ```
*/
function create_marker(location) {
const marker = new Marker({
draggable: true
})
.setLngLat(location)
.addTo(map);

marker.getElement().addEventListener("contextmenu", (e) => {
if (e.button === 2) {
const index = markers.findIndex((m) => m === marker);
markers[index].remove();
markers.splice(index, 1);
}
});
return marker;
}

/**
* Redraws all the markers on the map.
*
* This function will mutate the markers variable.
* */
Expand All @@ -187,12 +218,9 @@ export function redraw_markers() {
const location = line_coords[i];

// Adding draggable markers
markers[i] = new Marker({
draggable: true,
})
.setLngLat(location)
.addTo(map);
markers[i] = create_marker(location);
}

if (markers.length > 1) {
const start = markers[0].getElement();
start.replaceChildren(start_image);
Expand All @@ -201,22 +229,4 @@ export function redraw_markers() {
}
}

/** Addas a new marker to the map.
*
* @param {import("maplibre-gl/dist/maplibre-gl.js").LngLatLike} location The location of the new marker.
* */
function add_marker(location) {
const marker_line_index = markers.length;
logging.debug(`Line Marker Index: ${marker_line_index.toString()}`);

// Adding draggable markers
const marker = new Marker({
draggable: true
})
.setLngLat(location)
.addTo(map);

markers.push(marker);
}

export default path_data;