Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@0verIords
Last active May 27, 2020 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0verIords/771763acde0aa9bb31c7bb79d57926a9 to your computer and use it in GitHub Desktop.
Save 0verIords/771763acde0aa9bb31c7bb79d57926a9 to your computer and use it in GitHub Desktop.
type response struct {
Response matrixResponse `json:"response"`
}
type matrixResponse struct {
Route []matrixRoute `json:"route"`
}
type matrixRoute struct {
Summary summary `json:"summary"`
}
type summary struct {
Distance int `json:"distance"`
TrafficTime int `json:"trafficTime"`
}
func HereDistanceETA() (response, error) {
matrixResponse := response{}
query := fmt.Sprintf("&waypoint%v=geo!%v,%v", 0, from.Lat, from.Lon)
query += fmt.Sprintf("&waypoint%v=geo!%v,%v", 1, to.Lat, to.Lon)
query += "&mode=fastest;car;traffic:enabled"
url := fmt.Sprintf("https://route.ls.hereapi.com/routing/7.2/calculateroute.json?apiKey=%s", h.hereAPIKey)
url += query
resp, err := http.Get(url)
if err != nil {
logrus.WithFields(logrus.Fields{
"url": url,
"error": err,
}).Error("Get here response failed")
return durationDistancePair{}, err
}
if resp.StatusCode != http.StatusOK {
return durationDistancePair{}, fmt.Errorf("Here service, status code %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return durationDistancePair{}, err
}
err = json.Unmarshal(body, &matrixResponse)
if err != nil {
return durationDistancePair{}, err
}
if len(matrixResponse.Response.Route) == 0 {
return durationDistancePair{}, errors.New("Matrix response empty")
}
res := durationDistancePair{
dur: time.Duration(matrixResponse.Response.Route[0].Summary.TrafficTime) * time.Second,
distanceMeters: matrixResponse.Response.Route[0].Summary.Distance,
}
return res, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment