Last active
June 19, 2019 07:46
-
-
Save armand1m/275afd439607f0d4e4d7cebddbf07e36 to your computer and use it in GitHub Desktop.
Geocoder Implementation using OSM Nominatim
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
import fetch from "node-fetch"; | |
interface Location { | |
place_id: number; | |
licence: string; | |
osm_type: string; | |
osm_id: number; | |
boundingbox: string[]; | |
lat: string; | |
lon: string; | |
display_name: string; | |
class: string; | |
type: string; | |
importance: number; | |
icon: string; | |
}; | |
const geosearch = async (q: string, limit: string = "1") => { | |
const params = new URLSearchParams({ | |
q, | |
limit, | |
format: "json" | |
}); | |
const ENDPOINT = `https://nominatim.openstreetmap.org/search?${params.toString()}`; | |
const payload: Location[] = await fetch(ENDPOINT).then(res => res.json()); | |
if (!payload || !payload.length) { | |
throw new Error(`No response for Address: ${q}`); | |
} | |
return payload; | |
}; | |
export default geosearch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment