-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8eb03cf
commit cd5dfb9
Showing
4 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { useState, useCallback } from "react"; | ||
import classNames from "classnames"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { faSearch } from "@fortawesome/free-solid-svg-icons"; | ||
import { useMap } from "react-map-gl/maplibre"; | ||
import debounce from "lodash/debounce"; | ||
import isEmpty from "lodash/isEmpty"; | ||
import ReactHtmlParser from "react-html-parser"; | ||
|
||
const BASE_URL = "https://api3.geo.admin.ch/rest/services/api/SearchServer"; | ||
|
||
interface SearchResult { | ||
bbox: number[]; | ||
features: SearchFeature[]; | ||
} | ||
|
||
interface SearchFeature { | ||
bbox: number[]; | ||
geometry: { | ||
coordinates: number[]; | ||
type: string; | ||
}; | ||
id: number | string; | ||
properties: { | ||
detail: string; | ||
label: string; | ||
rank: number; | ||
type: string; | ||
geom_quadindex: string; | ||
lat: number; | ||
lon: number; | ||
objectclass: string; | ||
origin: string; | ||
weight: number; | ||
x: number; | ||
y: number; | ||
zoomlevel: number; | ||
}; | ||
} | ||
|
||
function SearchControl() { | ||
const { current: map } = useMap(); | ||
const [searchResults, setSearchResults] = useState<SearchFeature[]>([]); | ||
const [input, setInput] = useState<string>(""); | ||
|
||
const flyTo = useCallback( | ||
(target: SearchFeature) => { | ||
map?.flyTo({ | ||
center: [target.properties.lon, target.properties.lat], | ||
zoom: 17, | ||
animate: true, | ||
duration: 2500, | ||
}); | ||
setSearchResults([]); | ||
setInput(""); | ||
}, | ||
[map], | ||
); | ||
|
||
const search = (input: string) => { | ||
fetch( | ||
BASE_URL + | ||
"?" + | ||
new URLSearchParams({ | ||
searchText: input, | ||
type: "locations", | ||
geometryFormat: "geojson", | ||
origins: "address,gazetteer,parcel", | ||
limit: "10", | ||
}), | ||
) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
const searchResult: SearchResult = { | ||
bbox: data.bbox, | ||
features: data.features, | ||
}; | ||
setSearchResults(searchResult.features); | ||
}); | ||
}; | ||
const debouncedSearch = debounce(search, 1000); | ||
|
||
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = e.target.value; | ||
setInput(value); | ||
debouncedSearch(value); | ||
}; | ||
|
||
const dropdown = classNames({ | ||
dropdown: true, | ||
"is-active": !isEmpty(searchResults), | ||
}); | ||
|
||
return ( | ||
<div className="is-flex is-justify-content-center is-align-content-center mt-3"> | ||
<div className={dropdown}> | ||
<div className="dropdown-trigger"> | ||
<div className="field has-addons"> | ||
<div className="control is-expanded has-icons-left"> | ||
<span className="icon is-left"> | ||
<FontAwesomeIcon icon={faSearch} /> | ||
</span> | ||
<input | ||
className="input" | ||
type="search" | ||
value={input} | ||
placeholder="" | ||
onChange={onChange} | ||
onKeyDown={(e) => e.key === "Enter" && search(input)} | ||
/> | ||
</div> | ||
</div> | ||
<div className="dropdown-menu" id="dropdown-menu"> | ||
<div className="dropdown-content"> | ||
{searchResults && | ||
searchResults.map((result: SearchFeature) => ( | ||
<a onClick={() => flyTo(result)} key={result.id} className="dropdown-item"> | ||
{ReactHtmlParser(result.properties.label)} | ||
</a> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default SearchControl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters