-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathApp.js
55 lines (39 loc) · 1.38 KB
/
App.js
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
import './App.css';
// react
import React, { useState, useEffect } from 'react';
// openlayers
import GeoJSON from 'ol/format/GeoJSON'
// components
import MapWrapper from './components/MapWrapper'
function App() {
// set intial state
const [ features, setFeatures ] = useState([])
// initialization - retrieve GeoJSON features from Mock JSON API get features from mock
// GeoJson API (read from flat .json file in public directory)
useEffect( () => {
fetch('/mock-geojson-api.json')
.then(response => response.json())
.then( (fetchedFeatures) => {
// parse fetched geojson into OpenLayers features
// use options to convert feature from EPSG:4326 to EPSG:3857
const wktOptions = {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
}
const parsedFeatures = new GeoJSON().readFeatures(fetchedFeatures, wktOptions)
// set features into state (which will be passed into OpenLayers
// map component as props)
setFeatures(parsedFeatures)
})
},[])
return (
<div className="App">
<div className="app-label">
<p>React Functional Components with OpenLayers Example</p>
<p>Click the map to reveal location coordinate via React State</p>
</div>
<MapWrapper features={features} />
</div>
)
}
export default App