-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapquest.go
115 lines (103 loc) · 3.53 KB
/
mapquest.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package p
import (
"encoding/json"
"fmt"
"os"
)
// locationMapquestStruct: https://developer.mapquest.com/documentation/open/geocoding-api/address/get/
// Autogenerated from https://mholt.github.io/json-to-go/
type locationMapquestStruct struct {
Info struct {
Statuscode int `json:"statuscode"`
Copyright struct {
Text string `json:"text"`
ImageURL string `json:"imageUrl"`
ImageAltText string `json:"imageAltText"`
} `json:"copyright"`
Messages []interface{} `json:"messages"`
} `json:"info"`
Options struct {
MaxResults int `json:"maxResults"`
ThumbMaps bool `json:"thumbMaps"`
IgnoreLatLngInput bool `json:"ignoreLatLngInput"`
} `json:"options"`
Results []struct {
ProvidedLocation struct {
Location string `json:"location"`
} `json:"providedLocation"`
Locations []struct {
Street string `json:"street"`
AdminArea6 string `json:"adminArea6"`
AdminArea6Type string `json:"adminArea6Type"`
AdminArea5 string `json:"adminArea5"`
AdminArea5Type string `json:"adminArea5Type"`
AdminArea4 string `json:"adminArea4"`
AdminArea4Type string `json:"adminArea4Type"`
AdminArea3 string `json:"adminArea3"`
AdminArea3Type string `json:"adminArea3Type"`
AdminArea1 string `json:"adminArea1"`
AdminArea1Type string `json:"adminArea1Type"`
PostalCode string `json:"postalCode"`
GeocodeQualityCode string `json:"geocodeQualityCode"`
GeocodeQuality string `json:"geocodeQuality"`
DragPoint bool `json:"dragPoint"`
SideOfStreet string `json:"sideOfStreet"`
LinkID string `json:"linkId"`
UnknownInput string `json:"unknownInput"`
Type string `json:"type"`
LatLng struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"latLng"`
DisplayLatLng struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"displayLatLng"`
MapURL string `json:"mapUrl"`
} `json:"locations"`
} `json:"results"`
}
// MapquestProvider provider
type MapquestProvider struct {
URL string
Key string
Timeout int
}
// NewMapquest creates a integration with Mapquest provider
func NewMapquest() (MapquestProvider, error) {
var key = os.Getenv("mapquest_key")
var url = "http://www.mapquestapi.com"
if len(key) == 0 {
return MapquestProvider{}, fmt.Errorf("mapquest_key variable not defined")
}
return MapquestProvider{
URL: url,
Key: key,
Timeout: 10,
}, nil
}
// GetLocationFromName : Get Location using a name as direction
func (m MapquestProvider) GetLocationFromName(name string) (LocationAPIV1Struct, error) {
url := fmt.Sprintf("%s/geocoding/v1/address?key=%s&location=%s", m.URL, m.Key, name)
// fetch api, returns body
locationByte, err := fetchAPI(url, m.Timeout)
if err != nil {
return LocationAPIV1Struct{}, err
}
// Convert body to location mapquest struct
location := locationMapquestStruct{}
err = json.Unmarshal(locationByte, &location)
if err != nil {
return LocationAPIV1Struct{}, err
}
// Convert location mapquest struct to location api v1
return m.locationAPIV1(location)
}
// locationAPIV1 location struct convert
func (m *MapquestProvider) locationAPIV1(location locationMapquestStruct) (LocationAPIV1Struct, error) {
var res LocationAPIV1Struct
res.Name = location.Results[0].ProvidedLocation.Location
res.LatLng.Lat = location.Results[0].Locations[0].LatLng.Lat
res.LatLng.Lng = location.Results[0].Locations[0].LatLng.Lng
return res, nil
}