-
Notifications
You must be signed in to change notification settings - Fork 1
/
map-full-plus-find-elevation.jsx
61 lines (53 loc) · 1.54 KB
/
map-full-plus-find-elevation.jsx
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
57
58
59
60
61
import React, { useEffect } from 'react'
import Findelevation from '../components/Findelevation/Findelevation'
import axios from 'axios'
import Layout from 'components/Layout'
import Map from 'components/Map'
import { siteMetadata } from '../../gatsby-config'
const { mapboxToken } = siteMetadata
const MapPage = () => {
const findElevation = () => {
map.on('click', function(e) {
var query =
'https://api.mapbox.com/v4/mapbox.mapbox-terrain-v2/tilequery/' +
e.lngLat.lng +
',' +
e.lngLat.lat +
'.json?layers=contour&limit=50&access_token=' +
mapboxToken
// https://stackoverflow.com/questions/50949594/axios-having-cors-issue
axios.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded'
axios
.get(query)
.then(function(response) {
var elevations = []
response.data.features.forEach(function(feature) {
elevations.push(feature.properties.ele)
})
document.getElementById('findelevationfield').textContent = Math.max(
...elevations
)
})
.catch(function(error) {
console.log(error)
})
.then(function() {
// immer
})
})
}
useEffect(() => {
window.addEventListener('click', findElevation)
return () => {
window.removeEventListener('click', findElevation)
}
}, [])
return (
<Layout title="Full Screen Map">
<Findelevation />
<Map />
</Layout>
)
}
export default MapPage