Basic nominatim lookups.

master
Sander Vocke 6 years ago
parent 21040bca36
commit e6d5f8f754
  1. 35
      src/map.js
  2. 29
      src/searchbar.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 (
<Box>
<div id={id} style={style}></div>
</Box>
<>
<Box>
<SearchBar onSubmit={onSearch} />
</Box>
<Box>
<div id={id} style={style}></div>
</Box>
</>
);
}

@ -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 (
<>
<TextField variant="outlined" fullWidth label="Search" type="text" onChange={this.onChangeHandler} onKeyDown={this.keyDownHandler} />
</>
);
}
return (
<>
<TextField variant="outlined" fullWidth label="Search" type="text" onChange={onChangeHandler} onKeyDown={keyDownHandler} />
</>
);
}
Loading…
Cancel
Save