geocode/pelias.go

42 lines
735 B
Go
Raw Permalink Normal View History

2023-03-29 10:51:49 +00:00
package geocode
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/paulmach/orb/geojson"
)
const (
PELIAS_AUTOCOMPLETE_ENDPOINT = "/v1/autocomplete"
)
type PeliasHandler struct {
BaseURL string
}
func NewPeliasHandler(baseUrl string) (PeliasHandler, error) {
return PeliasHandler{
BaseURL: baseUrl,
}, nil
}
func (h PeliasHandler) Autocomplete(text string) (*geojson.FeatureCollection, error) {
url := fmt.Sprintf("%s%s?text=%s", h.BaseURL, PELIAS_AUTOCOMPLETE_ENDPOINT, url.QueryEscape(text))
resp, err := http.Get(url)
if err != nil {
return nil, err
}
fc := geojson.NewFeatureCollection()
if err = json.NewDecoder(resp.Body).Decode(&fc); err != nil {
return nil, err
}
return fc, nil
}