Skip to content

Instantly share code, notes, and snippets.

@mikepaszkiewicz
Created January 31, 2020 17:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mikepaszkiewicz/a8158a497f8810512534143edf83eaf4 to your computer and use it in GitHub Desktop.
Pelias vs Google Maps
import axios from 'axios'
interface GeocodeFeature {
"type": "Feature",
"geometry": {
"type": string,
"coordinates": number[]
},
"properties": {
"label": string //full address we want to use
"layer": string
"name": string
"housenumber": string
"street": string
"confidence": number,
"match_type": string
"distance": number,
"accuracy": string
"country": string
"country_a": string
"region": string
"region_a": string
"county": string
"locality": string
"neighbourhood": string
"continent": string
}
}
interface GeocodeRes {
bbox: number[]
feature: GeocodeFeature
}
export function encodeHttpAuth(username: string, password: string) {
return 'Basic ' + Buffer.from(`${username}:${password}`).toString(
'base64'
)
}
export async function geocode(address: string, territory_bounds?: string): Promise<GeocodeRes> {
const baseUrl = `https://pelias-philadelphia.tryhabitat.com`
const urlEndpoint = `/v1/search?text=${encodeURIComponent(address)}`
//TODO: implement territory BBOX const options = '&boundary.rect.min_lat=25.84&boundary.rect.min_lon=-106.65&boundary.rect.max_lat=36.5&boundary.rect.max_lon=-93.51'
const request = `${baseUrl}${urlEndpoint}`
const { data: { bbox, features } } = await axios.post(request, {}, {
headers: {
'Authorization': encodeHttpAuth(
process.env.ENGINE_USERNAME,
process.env.ENGINE_PASSWORD
)
}
})
const res = { bbox, feature: features[0] }
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment