Created
January 31, 2020 17:35
-
-
Save mikepaszkiewicz/a8158a497f8810512534143edf83eaf4 to your computer and use it in GitHub Desktop.
Pelias vs Google Maps
This file contains hidden or 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 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