Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* eslint-disable react-hooks/rules-of-hooks */
import React, { useState, useEffect } from "react"
import { isArray } from "lodash"
import {
MapContainer,
LayersControl,
TileLayer,
WMSTileLayer,
GeoJSON,
} from "react-leaflet"
import "leaflet/dist/leaflet.css"
interface Props {
idMap: number;
}
const Map = (props: Props) => {
const projectUrl = process.env.PROJECT_URL as string
if (typeof window !== "undefined") {
let className = "z-40 h-full w-full"
// Default id map (from mapstore)
const idMap = props.idMap
// Default center, zoom
const center: number[] = [47.1, -1.25]
const zoom: number = 9
const layers: any[] = []
// Get current map Object when ready (whenCreated)
const [map, setMap] = useState(null)
// Init default map context
const [mapContext, setMapContext] = useState({
center: {
x: center[1],
y: center[0],
},
zoom: zoom,
layers: layers,
})
// Get Mapstore context
useEffect(() => {
fetch(projectUrl + "/geoportail/rest/geostore/data/" + idMap)
.then((response) => response.json())
.then((resultData) => {
setMapContext(resultData.map)
// Update map view with context data
if (map)
// @ts-ignore
map.setView(
[resultData.map.center.y, resultData.map.center.x],
resultData.map.zoom
)
})
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
}, [projectUrl, map, idMap])
return (
<>
<MapContainer
center={[mapContext.center.y, mapContext.center.x]}
zoom={mapContext.zoom}
// @ts-ignore
whenCreated={setMap}
className={className}
// scrollWheelZoom={false}
>
<LayersControl>
{
// BACKGROUNDS
mapContext.layers
.filter((layer) => layer.group === "background")
.map((layer, key) => {
switch (layer.type) {
case "osm":
return (
<LayersControl.BaseLayer
key={key}
checked={layer.visibility}
name={layer.title}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</LayersControl.BaseLayer>
)
case "wmts":
// URL for IGN WMTS services
// TODO: test for other WMTS
const url =
layer.url +
"?service=WMTS&request=GetTile&version=1.0.0&tilematrixset=PM&tilematrix={z}&tilecol={x}&tilerow={y}&layer=" +
layer.name +
"&format=" +
layer.format +
"&style=" +
layer.style
return (
<LayersControl.BaseLayer
key={key}
checked={layer.visibility}
name={layer.title}
>
<TileLayer
url={url}
tileSize={256}
opacity={layer.opacity}
/>
</LayersControl.BaseLayer>
)
}
})
}
{
// OVERLAYS
// Load layers from context
mapContext.layers.map((layer, key) => {
if (isArray(layer.url)) return null
switch (layer.type) {
case "vector":
return (
<LayersControl.Overlay
key={key}
checked={layer.visibility}
name={layer.name}
>
<GeoJSON
data={layer.features}
pathOptions={layer.style || ""}
/>
</LayersControl.Overlay>
)
case "wfs":
break
case "wms":
return (
<LayersControl.Overlay
key={key}
checked={layer.visibility}
name={layer.title}
>
<WMSTileLayer
layers={layer.name}
format={layer.format}
transparent={true}
url={layer.url}
styles={layer.style || ""}
opacity={layer.opacity}
/>
</LayersControl.Overlay>
)
}
return null
})
}
</LayersControl>
</MapContainer>
</>
)
}
return null
}
export default Map