|
1
|
|
|
package yageocoding |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"fmt" |
|
5
|
|
|
"net/http" |
|
6
|
|
|
"encoding/json" |
|
7
|
|
|
"errors" |
|
8
|
|
|
"strings" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
type YaGeoResponse struct { |
|
|
|
|
|
|
12
|
|
|
Response struct { |
|
13
|
|
|
ObjectCollection YaGeoObjectCollection `json:"GeoObjectCollection"` |
|
14
|
|
|
} `json:"response"` |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
type YaGeoObjectCollection struct { |
|
|
|
|
|
|
18
|
|
|
MetaData YaGeoMetaData `json:"metaDataProperty"` |
|
19
|
|
|
Member []YaGeoMember `json:"featureMember"` |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
type YaGeoMetaData struct { |
|
|
|
|
|
|
23
|
|
|
ResponseMetaData struct { |
|
24
|
|
|
Request string `json:"request"` |
|
25
|
|
|
Found string `json:"found"` |
|
26
|
|
|
Results string `json:"results"` |
|
27
|
|
|
} `json:"GeocoderResponseMetaData"` |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
type YaGeoMember struct { |
|
|
|
|
|
|
31
|
|
|
GeoObject struct { |
|
32
|
|
|
MetaData YaGeoMemberMetaData `json:"metaDataProperty"` |
|
33
|
|
|
Description string `json:"description"` |
|
34
|
|
|
Name string `json:"name"` |
|
35
|
|
|
Point struct { |
|
36
|
|
|
Pos string `json:"pos"` |
|
37
|
|
|
} `json:"Point"` |
|
38
|
|
|
} `json:"GeoObject"` |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
type YaGeoMemberMetaData struct { |
|
|
|
|
|
|
42
|
|
|
Meta struct { |
|
43
|
|
|
Kind string `json:"kind"` |
|
44
|
|
|
Text string `json:"text"` |
|
45
|
|
|
Precision string `json:"precision"` |
|
46
|
|
|
} `json:"GeocoderMetaData"` |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
type YaGeoAddress struct { |
|
|
|
|
|
|
50
|
|
|
CountryCode string `json:"country_code"` |
|
51
|
|
|
PostalCode string `json:"postal_code"` |
|
52
|
|
|
Formatted string `json:"formatted"` |
|
53
|
|
|
Components []YaGeoAddressComponent `json:"Components"` |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
type YaGeoAddressComponent struct { |
|
|
|
|
|
|
57
|
|
|
Kind string `json:"kind"` |
|
58
|
|
|
Name string `json:"name"` |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
func (loc *YaGeoResponse) GetCoordinates() (string, string) { |
|
|
|
|
|
|
62
|
|
|
if len(loc.Response.ObjectCollection.Member) == 0 { |
|
63
|
|
|
return "0", "0" |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
str := strings.Split(loc.Response.ObjectCollection.Member[0].GeoObject.Point.Pos, " ") |
|
67
|
|
|
return str[0], str[1] |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
func GetData(key, location string) (result YaGeoResponse, err error) { |
|
|
|
|
|
|
71
|
|
|
resp, err := http.Get(fmt.Sprintf("https://geocode-maps.yandex.ru/1.x/?format=json&geocode=%v&apikey=%v", location, key)) |
|
72
|
|
|
if err != nil { |
|
73
|
|
|
return |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
err = json.NewDecoder(resp.Body).Decode(&result) |
|
77
|
|
|
if err != nil { |
|
78
|
|
|
return result, err |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if len(result.Response.ObjectCollection.Member) == 0 { |
|
82
|
|
|
return result, errors.New("location not fount") |
|
83
|
|
|
} |
|
84
|
|
|
return |
|
85
|
|
|
} |
|
86
|
|
|
|