-
-
Save 0verIords/771763acde0aa9bb31c7bb79d57926a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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