125 lines
3.4 KiB
Go
125 lines
3.4 KiB
Go
|
package yaGeo
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
type GeoObject struct {
|
||
|
MetaDataProperty struct {
|
||
|
GeocoderMetaData struct {
|
||
|
Precision string `json:"precision"`
|
||
|
Text string `json:"text"`
|
||
|
Kind string `json:"kind"`
|
||
|
Address struct {
|
||
|
CountryCode string `json:"country_code"`
|
||
|
Formatted string `json:"formatted"`
|
||
|
PostalCode string `json:"postal_code,omitempty"`
|
||
|
Components []struct {
|
||
|
Kind string `json:"kind"`
|
||
|
Name string `json:"name"`
|
||
|
} `json:"Components"`
|
||
|
} `json:"Address"`
|
||
|
AddressDetails struct {
|
||
|
Country struct {
|
||
|
AddressLine string `json:"AddressLine"`
|
||
|
CountryNameCode string `json:"CountryNameCode"`
|
||
|
CountryName string `json:"CountryName"`
|
||
|
AdministrativeArea struct {
|
||
|
AdministrativeAreaName string `json:"AdministrativeAreaName"`
|
||
|
SubAdministrativeArea struct {
|
||
|
SubAdministrativeAreaName string `json:"SubAdministrativeAreaName"`
|
||
|
Locality struct {
|
||
|
LocalityName string `json:"LocalityName"`
|
||
|
Thoroughfare struct {
|
||
|
ThoroughfareName string `json:"ThoroughfareName"`
|
||
|
Premise struct {
|
||
|
PremiseNumber string `json:"PremiseNumber"`
|
||
|
PostalCode struct {
|
||
|
PostalCodeNumber string `json:"PostalCodeNumber"`
|
||
|
} `json:"PostalCode,omitempty"`
|
||
|
} `json:"Premise"`
|
||
|
} `json:"Thoroughfare,omitempty"`
|
||
|
DependentLocality struct {
|
||
|
DependentLocalityName string `json:"DependentLocalityName"`
|
||
|
Thoroughfare struct {
|
||
|
ThoroughfareName string `json:"ThoroughfareName"`
|
||
|
Premise struct {
|
||
|
PremiseNumber string `json:"PremiseNumber"`
|
||
|
} `json:"Premise"`
|
||
|
} `json:"Thoroughfare"`
|
||
|
} `json:"DependentLocality,omitempty"`
|
||
|
} `json:"Locality"`
|
||
|
} `json:"SubAdministrativeArea"`
|
||
|
} `json:"AdministrativeArea"`
|
||
|
} `json:"Country"`
|
||
|
} `json:"AddressDetails"`
|
||
|
} `json:"GeocoderMetaData"`
|
||
|
} `json:"metaDataProperty"`
|
||
|
Name string `json:"name"`
|
||
|
Description string `json:"description"`
|
||
|
BoundedBy struct {
|
||
|
Envelope struct {
|
||
|
LowerCorner string `json:"lowerCorner"`
|
||
|
UpperCorner string `json:"upperCorner"`
|
||
|
} `json:"Envelope"`
|
||
|
} `json:"boundedBy"`
|
||
|
Uri string `json:"uri"`
|
||
|
Point struct {
|
||
|
Pos string `json:"pos"`
|
||
|
} `json:"Point"`
|
||
|
}
|
||
|
|
||
|
type geoResponseWrapper struct {
|
||
|
GeoResponse `json:"response"`
|
||
|
}
|
||
|
|
||
|
type geoObjectCollection struct {
|
||
|
FeatureMember []struct {
|
||
|
GeoObject `json:"GeoObject"`
|
||
|
} `json:"featureMember"`
|
||
|
}
|
||
|
type GeoResponse struct {
|
||
|
geoObjectCollection `json:"GeoObjectCollection"`
|
||
|
}
|
||
|
|
||
|
type yaGeo struct {
|
||
|
EP string
|
||
|
apiKey string
|
||
|
}
|
||
|
|
||
|
type YaGeo interface {
|
||
|
GeoCode(q string) (*[]GeoObject, error)
|
||
|
}
|
||
|
|
||
|
func Init() YaGeo {
|
||
|
return &yaGeo{
|
||
|
EP: "https://geocode-maps.yandex.ru/1.x/",
|
||
|
apiKey: os.Getenv("YANDEX_GEOCODER_API_KEY"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (y *yaGeo) GeoCode(q string) (*[]GeoObject, error) {
|
||
|
data := new(geoResponseWrapper)
|
||
|
req, _ := http.NewRequest("GET", y.EP, nil)
|
||
|
params := req.URL.Query()
|
||
|
params.Add("apikey", y.apiKey)
|
||
|
params.Add("geocode", q)
|
||
|
params.Add("lang", "ru_RU")
|
||
|
params.Add("format", "json")
|
||
|
req.URL.RawQuery = params.Encode()
|
||
|
|
||
|
resp, err := http.Get(req.URL.String())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
err = json.NewDecoder(resp.Body).Decode(data)
|
||
|
items := []GeoObject{}
|
||
|
|
||
|
for _, d := range data.GeoResponse.geoObjectCollection.FeatureMember {
|
||
|
items = append(items, d.GeoObject)
|
||
|
}
|
||
|
return &items, err
|
||
|
}
|