diff --git a/src/map.js b/src/map.js index 52f1ef1..67e9b22 100644 --- a/src/map.js +++ b/src/map.js @@ -1,10 +1,10 @@ -// TODO https://leafletjs.com/examples/quick-start/ - import React, { useEffect, useState } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Box from '@material-ui/core/Box'; +import { SearchBar } from './searchbar.js'; + import "leaflet/dist/leaflet.css" import L from 'leaflet'; @@ -45,7 +45,25 @@ export function MapView(props) { updateCamera(); }); } - }, [ map ]) + }, [map]) + + function showNominatimResult(result) { + var corner1 = L.latLng(result.boundingbox[0], result.boundingbox[2]); + var corner2 = L.latLng(result.boundingbox[1], result.boundingbox[3]); + var bounds = L.latLngBounds(corner1, corner2); + map.flyToBounds(bounds); + } + + function onSearch(query) { + fetch("http://nominatim.openstreetmap.org/search?polygon_geojson=1&polygon_threshold=0.001&format=json&limit=5&q=" + query) + .then(res => res.json()) + .then(jsonres => { + console.log(jsonres); + if (Array.isArray(jsonres) && jsonres.length > 0) { + showNominatimResult(jsonres[0]); + } + }); + } const style = { width: "100%", @@ -53,8 +71,13 @@ export function MapView(props) { }; return ( - -
-
+ <> + + + + +
+
+ ); } \ No newline at end of file diff --git a/src/searchbar.js b/src/searchbar.js index a1b9a67..779f5f3 100644 --- a/src/searchbar.js +++ b/src/searchbar.js @@ -1,24 +1,21 @@ -import React from 'react'; +import React, { useState } from 'react'; import TextField from '@material-ui/core/TextField'; -export class SearchBar extends React.Component { - state = { - text_value: false, - } +export function SearchBar(props) { + const [ textValue, setTextValue ] = useState(false); + const { onSubmit } = props; - onSubmitHandler = () => { this.props.onSubmit(this.state.text_value); } - onChangeHandler = (e) => { this.setState({ text_value: e.target.value }); } - keyDownHandler = (e) => { + function onSubmitHandler() { onSubmit(textValue); } + function onChangeHandler(e) { setTextValue(e.target.value); } + function keyDownHandler(e) { if (e.key === 'Enter') { - this.onSubmitHandler(); + onSubmitHandler(); } } - render() { - return ( - <> - - - ); - } + return ( + <> + + + ); } \ No newline at end of file