Skip to content

Commit

Permalink
Fix geoman functionalities / Add geoman functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
JoonasAapro committed Jan 19, 2024
1 parent 2e712cc commit c40f9da
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/cells/decidim/locations/map_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def markers_data_for_map
address: data[4],
latitude: data[5],
longitude: data[6],
shape: data[7],
geojson: data[8],
link: path_for(data[0])
}
end
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/decidim/locations/locations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def format_map_locations(model)
# 6: Longitude of the record, e.g. 2.234
# 7: Shape of the record, e.g. "Polygon"
# 8: Geojson of the record, e.g. [[[30.0, 10.0], [12.2, 7.3]]]
# [123, "Title of the record", "Summary of the record", "Body text of the record", "Foobar street 123", 1.123, 2.234, "Shape of the record", "Geojson of the record"]
# [123, "Title of the record", "Summary of the record", "Body text of the record", "Foobar street 123",
# 1.123, 2.234, "Shape of the record", "Geojson of the record"]
title = translated_attribute(JSON.parse(record[1]))
body =
begin
Expand Down
13 changes: 8 additions & 5 deletions app/packs/src/decidim/locations/map/controller/location.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import MapController from "src/decidim/map/controller";
import markerIcon2x from "leaflet/dist/images/marker-icon-2x.png"
import markerIcon from "leaflet/dist/images/marker-icon.png"
import markerShadow from "leaflet/dist/images/marker-shadow.png"

export default class ModelLocMapController extends MapController {
start() {
Expand All @@ -8,12 +11,13 @@ export default class ModelLocMapController extends MapController {
}

initializeMap() {
// eslint-disable-next-line
delete L.Icon.Default.prototype._getIconUrl;

L.Icon.Default.mergeOptions({
iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'),
iconUrl: require('leaflet/dist/images/marker-icon.png'),
shadowUrl: require('leaflet/dist/images/marker-shadow.png'),
iconRetinaUrl: markerIcon2x,
iconUrl: markerIcon,
shadowUrl: markerShadow
});

const mapEl = this.map._container;
Expand Down Expand Up @@ -47,9 +51,8 @@ export default class ModelLocMapController extends MapController {
});

this.map.pm.setPathOptions(
{ color: "orange" },
{
ignoreShapes: ["Rectangle"]
ignoreShapes: ["Circle", "Rectangle"]
}
);
}
Expand Down
3 changes: 3 additions & 0 deletions app/packs/src/decidim/locations/map/integration/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export default () => {

shape.on("click", () => {
editModalEl.dataset.shapeId = shapeId;
ctrl.unbindPopup(shapeId);

const inputDiv = shapeFieldContainer.querySelector(`[data-shape-id="${shapeId}"]`);
if (inputDiv && !inputDiv.hasChildNodes()) {
Expand All @@ -208,6 +209,8 @@ export default () => {
});

shape.on("pm:dragend", () => {
ctrl.bindFetchPopup(shapeId);

if (objectShape === "Marker") {
$(mapEl).trigger("geocoder-reverse.decidim", [shape.getLatLng(), { shapeId, objectShape }]);
} else if (objectShape === "Polygon" || objectShape === "Line") {
Expand Down
96 changes: 96 additions & 0 deletions app/packs/src/decidim/map/controller/markers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import "src/decidim/vendor/jquery-tmpl"
import MapController from "src/decidim/map/controller"
import "leaflet.markercluster";
import markerIcon2x from "leaflet/dist/images/marker-icon-2x.png"
import markerIcon from "leaflet/dist/images/marker-icon.png"
import markerShadow from "leaflet/dist/images/marker-shadow.png"

export default class MapMarkersController extends MapController {
start() {
this.markerClusters = null;

if (Array.isArray(this.config.markers) && this.config.markers.length > 0) {
this.addMarkers(this.config.markers);
} else {
this.map.fitWorld();
}
}

addMarkers(markersData) {
// eslint-disable-next-line
delete L.Icon.Default.prototype._getIconUrl;

L.Icon.Default.mergeOptions({
iconRetinaUrl: markerIcon2x,
iconUrl: markerIcon,
shadowUrl: markerShadow
});

if (this.markerClusters === null) {
this.markerClusters = new L.MarkerClusterGroup();
this.map.addLayer(this.markerClusters);
}

// Pre-compiles the template
$.template(
this.config.popupTemplateId,
$(`#${this.config.popupTemplateId}`).html()
);

const bounds = new L.LatLngBounds(
markersData.map(
(markerData) => [markerData.latitude, markerData.longitude]
)
);

markersData.forEach((markerData) => {
let shape = {}
if (markerData.shape === "Marker") {
shape = L.marker([markerData.latitude, markerData.longitude], {
title: markerData.title
})
} else if (markerData.shape === "Line") {
shape = L.polyline(JSON.parse(markerData.geojson).map((coords) => [coords.lat, coords.lng]), {
title: markerData.title
})
} else if (markerData.shape === "Polygon") {
shape = L.polygon(JSON.parse(markerData.geojson).map((coord) => coord.map((coords) => [coords.lat, coords.lng])), {
title: markerData.title
})
}

let node = document.createElement("div");

$.tmpl(this.config.popupTemplateId, markerData).appendTo(node);
shape.bindPopup(node, {
maxwidth: 640,
minWidth: 500,
keepInView: true,
className: "map-info"
})

this.markerClusters.addLayer(shape);
});


// Make sure there is enough space in the map for the padding to be
// applied. Otherwise the map will automatically zoom out (test it on
// mobile). Make sure there is at least the same amount of width and
// height available on both sides + the padding (i.e. 4x padding in
// total).
const size = this.map.getSize();
if (size.y >= 400 && size.x >= 400) {
this.map.fitBounds(bounds, { padding: [100, 100] });
} else if (size.y >= 120 && size.x >= 120) {
this.map.fitBounds(bounds, { padding: [30, 30] });
} else {
this.map.fitBounds(bounds);
}
}

clearMarkers() {
this.map.removeLayer(this.markerClusters);
this.markerClusters = new L.MarkerClusterGroup();
this.map.addLayer(this.markerClusters);
}
}

0 comments on commit c40f9da

Please sign in to comment.