searchLocation - retrieve OSM search results

This commit is contained in:
mmacierzanka committed 2021-10-02 15:41:19 +02:00
1 parent dfba455dcc
commit 211e882fe3
1 file changed
+24
+24
View File
@@ -0,0 +1,24 @@
import axios from "axios";
export const searchLocation = async (query) => {
if (typeof query !== "string") throw new TypeError("Query must be a string");
const param = encodeURIComponent(query);
const rq = await axios.get(`https://nominatim.openstreetmap.org/search?q=${param}&format=json&polygon_geojson=1&addressdetails=1`);
if (rq.status !== 200) throw new Error(`Server responded with status code ${rq.status}: ${rq.statusText}`);
try {
return rq.data
.map((result) => {
if (!result.lat || !result.lon) return null;
return {
name: result.display_name ?? "Search result",
street: `${result.address?.road + ", " ?? ""}${result.address?.city ?? ""}`,
lat: result.lat,
lon: result.lon,
};
})
.filter((result) => result !== null);
} catch (e) {
console.error(e);
return [];
}
};