S4WEB/riot/maMap.riot

87 lines
2.4 KiB
Plaintext
Raw Normal View History

<maMap>
<script>
export default function maMap() {
let markers = [];
let map;
return {
onBeforeMount(props, state) {
// création de la carte Leaflet
map = L.map("map");
map.fitWorld();
// ajout d'un calque de tuiles OpenStreetMap
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© OpenStreetMap contributors",
}).addTo(map);
// écouteur pour l'événement "moveend"
map.on("moveend", this.onMoveEnd);
},
onUpdated() {
if (this.props.records) {
this.removeMarks();
2023-03-31 16:31:25 +02:00
this.afficheMarks(this.props.records);
this.onMoveEnd();
}
},
removeMarks() {
markers.forEach((marker) => {
marker.remove();
});
},
2023-03-31 16:31:25 +02:00
afficheMarks(records) {
records.forEach((item) => {
var marker = L.marker([item.fields.g_olocalisation_des_formations[0], item.fields.g_olocalisation_des_formations[1]]).addTo(map);
marker.on('click', () => {
this.props.afficherModal(item)
});
markers.push(marker);
});
},
getVisibleMarkers() {
let visibleMarkers = [];
map.eachLayer((layer) => {
if (layer instanceof L.Marker && map.getBounds().contains(layer.getLatLng())) {
visibleMarkers.push(layer);
}
});
return visibleMarkers;
},
// méthode pour gérer l'événement "moveend"
onMoveEnd() {
let visibleMarkers = this.getVisibleMarkers();
// filtrer les éléments du tableau records qui ont une localisation correspondant à un marker visible
let recordsWithLoc = this.props.records.filter((record) => {
let loc = record.fields.g_olocalisation_des_formations;
for (let marker of visibleMarkers) {
if (marker.getLatLng().equals(L.latLng(loc[0], loc[1]))) {
return true;
}
}
return false;
});
let fin=[];
recordsWithLoc.forEach((forma) => {
fin.push(forma.recordid)
})
this.props.modifyByLoc(fin);
},
};
}
</script>
</maMap>