Skip to content

Commit 1c919c3

Browse files
authored
feat(front): add datatype in the url (#185)
1 parent 0575494 commit 1c919c3

File tree

6 files changed

+82
-20
lines changed

6 files changed

+82
-20
lines changed

docs/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## 🔖 0.3.0 (2025-09-04) - Mise à jour de données et ajout calque vulnérabilité à la chaleur
44

5+
### ✨ feat: Possibilité d'ouvrir la carte sur un calque spécifique
6+
7+
Le nom du calque est désormais codé dans l'url, ce qui permet de partager une vue spécifique de la carte, voilà par ex. l'url centrée sur Lyon centre avec les données de vulnérabilité à la chaleur : [carte.iarbre.fr/vulnerability/16/45.75773/4.85377](https://carte.iarbre.fr/vulnerability/16/45.75773/4.85377)
8+
9+
→ Ticket [#183](https://github.com/TelesCoop/iarbre/issues/183)
10+
511
### 🛠️ enhance: Données d'occupation des sols
612

713
- Ajout de nouvelles données : place PMR et d'autopartage.

front/cypress/e2e/mapInteractions.cy.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DataType, DataTypeToLabel } from "../../src/utils/enum"
44

55
describe("Map interactions", () => {
66
beforeEach(() => {
7-
cy.visit("/13/45.07126/5.5543")
7+
cy.visit("/plantability/13/45.07126/5.5543")
88
cy.get("@consoleInfo").should("have.been.calledWith", "cypress: map data loaded")
99
})
1010

@@ -21,6 +21,7 @@ describe("Map interactions", () => {
2121
cy.getBySel("plantability-score-label").should("exist")
2222

2323
cy.mapSwitchLayer(DataType.LOCAL_CLIMATE_ZONES) // cf. issue #142
24+
cy.url().should("include", "/lcz/")
2425
cy.getBySel("map-legend-title").should("contain", DataTypeToLabel[DataType.LOCAL_CLIMATE_ZONES])
2526
cy.mapHasNoPopup()
2627
cy.wait(200) // eslint-disable-line cypress/no-unnecessary-waiting
@@ -30,7 +31,11 @@ describe("Map interactions", () => {
3031
cy.mapOpenPopup() // cf. issue #92
3132

3233
cy.mapSwitchLayer(DataType.VULNERABILITY)
34+
cy.url().should("include", "/vulnerability/")
3335
cy.getBySel("map-legend-title").should("contain", DataTypeToLabel[DataType.VULNERABILITY])
3436
cy.mapHasNoPopup()
37+
38+
cy.visit("/lcz/13/45.07126/5.5543")
39+
cy.getBySel("map-legend-title").should("contain", DataTypeToLabel[DataType.LOCAL_CLIMATE_ZONES])
3540
})
3641
})

front/src/components/map/MapComponent.vue

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { useRouter, useRoute } from "vue-router"
55
import MapLegend from "@/components/map/legend/MapLegend.vue"
66
import MapScorePopup from "@/components/map/popup/MapScorePopup.vue"
77
import MapLayerSwitcher from "@/components/map/layerSwitcher/MapLayerSwitcher.vue"
8-
import { Map } from "maplibre-gl"
8+
import { updateMapRoute } from "@/utils/route"
9+
import { DataType } from "@/utils/enum"
910
1011
const router = useRouter()
1112
const route = useRoute()
@@ -17,35 +18,28 @@ const props = defineProps({
1718
}
1819
})
1920
20-
const updateRouteCoords = (map: Map) => {
21-
const coords = map.getCenter()
22-
router.replace({
23-
name: "mapWithCoords",
24-
params: {
25-
zoom: Math.round(map.getZoom()),
26-
lng: Math.round(100000 * coords.lng) / 100000,
27-
lat: Math.round(100000 * coords.lat) / 100000
28-
}
29-
})
30-
}
31-
3221
const mapStore = useMapStore()
3322
3423
onMounted(() => {
3524
mapStore.initMap(props.mapId)
3625
3726
if (route) {
3827
const mapInstance = mapStore.getMapInstance(props.mapId)
39-
if (route.name === "mapWithCoords") {
28+
if (route.name === "mapWithUrlParams") {
4029
const p = route.params
4130
mapInstance.jumpTo({
4231
center: [parseFloat(p.lng as string), parseFloat(p.lat as string)],
4332
zoom: parseFloat(p.zoom as string)
4433
})
34+
mapInstance.on("load", () => {
35+
mapStore.changeDataType(p.dataType as DataType)
36+
})
4537
}
4638
47-
mapInstance.on("moveend", () => updateRouteCoords(mapInstance))
48-
updateRouteCoords(mapInstance)
39+
mapInstance.on("moveend", () => {
40+
updateMapRoute(router, { map: mapInstance })
41+
})
42+
updateMapRoute(router, { map: mapInstance })
4943
}
5044
})
5145
</script>

front/src/components/map/layerSwitcher/LayerSwitcher.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
import { useMapStore } from "@/stores/map"
33
import { DataType, DataTypeToLabel } from "@/utils/enum"
44
import { computed } from "vue"
5+
import { updateMapRoute } from "@/utils/route"
6+
import { useRouter } from "vue-router"
57
68
const mapStore = useMapStore()
9+
const router = useRouter()
710
811
const selectedDataType = computed({
912
get: () => mapStore.selectedDataType,
1013
set: (value: DataType) => {
11-
console.log("changeLayer 0.0")
1214
mapStore.changeDataType(value)
15+
updateMapRoute(router, { dataType: mapStore.selectedDataType })
1316
}
1417
})
1518
</script>

front/src/router/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createRouter, createWebHistory } from "vue-router"
22
import MapView from "@/views/MapView.vue"
3+
import { DataType } from "@/utils/enum"
34

45
const router = createRouter({
56
history: createWebHistory(import.meta.env.BASE_URL),
@@ -10,9 +11,24 @@ const router = createRouter({
1011
component: MapView
1112
},
1213
{
13-
path: "/:zoom(\\d+)/:lat(\\d+.\\d+)/:lng(\\d+.\\d+)",
14-
name: "mapWithCoords",
14+
path: "/:dataType(plantability|lcz|vulnerability)/:zoom(\\d+)/:lat(\\d+.\\d+)/:lng(\\d+.\\d+)",
15+
name: "mapWithUrlParams",
1516
component: MapView
17+
},
18+
{
19+
path: "/:zoom(\\d+)/:lat(\\d+\\.\\d+)/:lng(\\d+\\.\\d+)",
20+
redirect: (to) => {
21+
const { zoom, lat, lng } = to.params
22+
return {
23+
name: "mapWithUrlParams",
24+
params: {
25+
dataType: DataType.PLANTABILITY,
26+
zoom,
27+
lat,
28+
lng
29+
}
30+
}
31+
}
1632
}
1733
]
1834
})

front/src/utils/route.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useRouter } from "vue-router"
2+
import { useMapStore } from "@/stores/map"
3+
import { Map } from "maplibre-gl"
4+
import type { DataType } from "./enum"
5+
6+
export function updateMapRoute(
7+
router: ReturnType<typeof useRouter>,
8+
options: {
9+
map?: Map
10+
dataType?: DataType
11+
}
12+
) {
13+
const mapStore = useMapStore()
14+
15+
const route = router.currentRoute.value
16+
17+
const { zoom, lat, lng } = options.map
18+
? {
19+
zoom: Math.round(options.map.getZoom()),
20+
lat: Math.round(100000 * options.map.getCenter().lat) / 100000,
21+
lng: Math.round(100000 * options.map.getCenter().lng) / 100000
22+
}
23+
: {
24+
zoom: route.params.zoom,
25+
lat: route.params.lat,
26+
lng: route.params.lng
27+
}
28+
29+
router.replace({
30+
name: "mapWithUrlParams",
31+
params: {
32+
dataType: options.dataType || mapStore.selectedDataType,
33+
zoom,
34+
lat,
35+
lng
36+
}
37+
})
38+
}

0 commit comments

Comments
 (0)