From bd700d878243c7fb1708835be5f42e566375482f Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 31 Jan 2023 21:39:07 +0100 Subject: [PATCH] now supports UTM and lat lon. --- src/components/finder/Finder.tsx | 67 +++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/src/components/finder/Finder.tsx b/src/components/finder/Finder.tsx index 5f76ecc..ca507f2 100644 --- a/src/components/finder/Finder.tsx +++ b/src/components/finder/Finder.tsx @@ -18,35 +18,55 @@ interface Props {} const Finder: Component = (props) => { const [open, setOpen] = createSignal(false); const [searchString, setSearchString] = createSignal(''); + const [popupContent, setPopupContent] = createSignal(<>); const navigate = useNavigate(); const [t, { add, locale, dict }] = useI18n(); - + let popup: Overlay | undefined; const searchStringChangeHandler = (event: any) => { setSearchString(event.target.value); }; const submitHandler = () => { let coordinates: Coordinate | undefined = undefined; - const utmRegExp = /([0-6]?[0-9])\s*([C-X])\s*([0-9]+)\s*([0-9]+)/; - const utmMatch = searchString().match(utmRegExp); - if (!!utmMatch) { - const utm = `+proj=utm +zone=${utmMatch[1]}`; - coordinates = proj4(utm, 'EPSG:4326', [+utmMatch[3], +utmMatch[4]]); + const latLonRegExp = /(-?[0-9]+.[0-9]+)\s*,\s*(-?[0-9]+.[0-9]+)/; + const latLonMatch = searchString().match(latLonRegExp); + + if (!!latLonMatch) { + coordinates = [+latLonMatch[2], +latLonMatch[1]]; + setPopupContent(<>{latLonMatch[0]}); + console.log({ + caller: 'Finder / submitHandler', + searchString: searchString(), + latLonMatch, + coordinates, + }); + } else { + const utmRegExp = /([0-6]?[0-9])\s*([C-X])\s*([0-9]+)\s*([0-9]+)/; + const utmMatch = searchString().match(utmRegExp); + if (!!utmMatch) { + const utm = `+proj=utm +zone=${utmMatch[1]}`; + coordinates = proj4(utm, 'EPSG:4326', [+utmMatch[3], +utmMatch[4]]); + setPopupContent( + <>{`${coordinates[0]}, ${coordinates[1]} (${utmMatch[0]})`} + ); + } + console.log({ + caller: 'Finder / submitHandler', + searchString: searchString(), + utmMatch, + coordinates, + }); } - console.log({ - caller: 'Finder / submitHandler', - searchString: searchString(), - isUtm: utmMatch, - coordinates, - }); if (!!coordinates) { setOpen(false); - const popup = new Overlay({ - element: document.getElementById('popup'), - positioning: 'center-center', - pos: fromLonLat(coordinates), - }); + if (popup === undefined) { + popup = new Overlay({ + element: document.getElementById('popup'), + positioning: 'center-center', + }); + } + popup.setMap(getMap()); popup.setPosition(fromLonLat(coordinates)); getMap()?.addOverlay(popup); @@ -58,6 +78,10 @@ const Finder: Component = (props) => { } }; + const popupCloseHandler = () => { + popup.setMap(null); + }; + return ( <>
@@ -92,8 +116,13 @@ const Finder: Component = (props) => { );