forked from codingsince1985/geo-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoder.go
85 lines (75 loc) · 2.04 KB
/
geocoder.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
// Package bing is a geo-golang based Microsoft Bing geocode/reverse geocode client
package bing
import (
"errors"
"fmt"
"strings"
"github.com/codingsince1985/geo-golang"
)
type (
baseURL string
geocodeResponse struct {
ResourceSets []struct {
Resources []struct {
Point struct {
Coordinates []float64
}
Address struct {
FormattedAddress string
AddressLine string
AdminDistrict string
AdminDistrict2 string
CountryRegion string
Locality string
PostalCode string
}
}
}
ErrorDetails []string
}
)
// Geocoder constructs Bing geocoder
func Geocoder(key string, baseURLs ...string) geo.Geocoder {
return geo.HTTPGeocoder{
EndpointBuilder: baseURL(getURL(key, baseURLs...)),
ResponseParserFactory: func() geo.ResponseParser { return &geocodeResponse{} },
}
}
func getURL(key string, baseURLs ...string) string {
if len(baseURLs) > 0 {
return baseURLs[0]
}
return "http://dev.virtualearth.net/REST/v1/Locations*key=" + key
}
func (b baseURL) GeocodeURL(address string) string {
return strings.Replace(string(b), "*", "?q="+address+"&", 1)
}
func (b baseURL) ReverseGeocodeURL(l geo.Location) string {
return strings.Replace(string(b), "*", fmt.Sprintf("/%f,%f?", l.Lat, l.Lng), 1)
}
func (r *geocodeResponse) Location() (*geo.Location, error) {
if len(r.ResourceSets) <= 0 || len(r.ResourceSets[0].Resources) <= 0 {
return nil, nil
}
c := r.ResourceSets[0].Resources[0].Point.Coordinates
return &geo.Location{
Lat: c[0],
Lng: c[1],
}, nil
}
func (r *geocodeResponse) Address() (*geo.Address, error) {
if len(r.ErrorDetails) > 0 {
return nil, errors.New(strings.Join(r.ErrorDetails, " "))
}
if len(r.ResourceSets) <= 0 || len(r.ResourceSets[0].Resources) <= 0 {
return nil, nil
}
a := r.ResourceSets[0].Resources[0].Address
return &geo.Address{
FormattedAddress: a.FormattedAddress,
Street: a.AddressLine,
City: a.Locality,
Postcode: a.PostalCode,
Country: a.CountryRegion,
}, nil
}