-
Notifications
You must be signed in to change notification settings - Fork 14
/
google_maps_provider.go
92 lines (80 loc) · 1.8 KB
/
google_maps_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package geocoding
import (
"context"
"googlemaps.github.io/maps"
)
type GoogleMapsProvider struct {
client *maps.Client
}
func NewGoogleMapsProvider(apiKey string) Provider {
client, err := maps.NewClient(maps.WithAPIKey(apiKey))
if err != nil {
panic(err)
}
return &GoogleMapsProvider{client: client}
}
func (g *GoogleMapsProvider) Geocode(ctx context.Context, address string, language Language) (*Address, interface{}, error) {
response, err := g.client.Geocode(
ctx,
&maps.GeocodingRequest{
Language: string(language),
Address: address,
})
if err != nil {
return nil, nil, err
}
if len(response) == 0 {
return &Address{
Address: address,
Language: language,
Location: &LatLng{
Lat: 0,
Lng: 0,
},
}, response, nil
}
return &Address{
Found: true,
Address: response[0].FormattedAddress,
Language: language,
Location: &LatLng{
Lat: response[0].Geometry.Location.Lat,
Lng: response[0].Geometry.Location.Lng,
},
}, response, nil
}
func (g *GoogleMapsProvider) ReverseGeocode(ctx context.Context, latLng *LatLng, language Language) (*Address, interface{}, error) {
response, err := g.client.ReverseGeocode(
ctx,
&maps.GeocodingRequest{
Language: string(language),
LatLng: &maps.LatLng{
Lat: latLng.Lat,
Lng: latLng.Lng,
},
})
if err != nil {
return nil, nil, err
}
if len(response) == 0 {
return &Address{
Language: language,
Location: &LatLng{
Lat: latLng.Lat,
Lng: latLng.Lng,
},
}, response, nil
}
return &Address{
Found: true,
Address: response[0].FormattedAddress,
Language: language,
Location: &LatLng{
Lat: response[0].Geometry.Location.Lat,
Lng: response[0].Geometry.Location.Lng,
},
}, response, nil
}
func (g *GoogleMapsProvider) GetName() string {
return "google_maps"
}