42 lines
735 B
Go
42 lines
735 B
Go
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
|
|
}
|