Skip to content

Instantly share code, notes, and snippets.

@armand1m
Last active June 19, 2019 07:46
Show Gist options
  • Save armand1m/275afd439607f0d4e4d7cebddbf07e36 to your computer and use it in GitHub Desktop.
Save armand1m/275afd439607f0d4e4d7cebddbf07e36 to your computer and use it in GitHub Desktop.
Geocoder Implementation using OSM Nominatim
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