find-stone-beer / files / bing-maps.go
bing-maps.go
577ed62e 2023-08-27 Pavel Safronov | 1 | package main |
| 2 | ||
| 3 | import ( | |
| 4 | "encoding/json" | |
| 5 | "fmt" | |
| 6 | "os" | |
| 7 | ||
| 8 | "github.com/jftuga/geodist" | |
| 9 | ) | |
| 10 | ||
| 11 | const ( | |
| 12 | BingMapsKeyEnvVar = "BING_MAPS_KEY" | |
| 13 | ) | |
| 14 | ||
| 15 | func GetDistBetweenPoints(a, b geodist.Coord) (float64, error) { | |
| 16 | dist, err := _getDistBetweenPoints(a.Lat, a.Lon, b.Lat, b.Lon) | |
| 17 | return dist, err | |
| 18 | } | |
| 19 | ||
| 20 | func _getDistBetweenPoints(lat1, lon1, lat2, lon2 float64) (float64, error) { | |
| 21 | // http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1={wayPoint1}&viaWaypoint.2={viaWaypoint2}&waypoint.3={waypoint3}&wayPoint.n={waypointN}&heading={heading}&optimize={optimize}&avoid={avoid}&distanceBeforeFirstTurn={distanceBeforeFirstTurn}&routeAttributes={routeAttributes}&timeType={timeType}&dateTime={dateTime}&maxSolutions={maxSolutions}&tolerances={tolerances}&distanceUnit={distanceUnit}&key={BingMapsKey} | |
| 22 | ||
| 23 | bingMapsKey, found := os.LookupEnv(BingMapsKeyEnvVar) | |
| 24 | if !found { | |
| 25 | return 0, fmt.Errorf("missing environment variable: %s", BingMapsKeyEnvVar) | |
| 26 | } | |
| 27 | ||
| 28 | url := fmt.Sprintf("http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1=%f,%f&wayPoint.2=%f,%f&distanceUnit=mi&key=%s", lat1, lon1, lat2, lon2, bingMapsKey) | |
| 29 | data, err := getCachedUrl( | |
| 30 | url, | |
| 31 | fmt.Sprintf("bing-maps-%f,%f,%f,%f", lat1, lon1, lat2, lon2), | |
| 32 | "2006-01", // cache this data for 1 month | |
| 33 | ) | |
| 34 | if err != nil { | |
| 35 | return 0, err | |
| 36 | } | |
| 37 | ||
| 38 | autoGenerated := autoGenerated{} | |
| 39 | err = json.Unmarshal(data, &autoGenerated) | |
| 40 | if err != nil { | |
6cc7663b 2023-08-28 Pavel Safronov | 41 | return 0, fmt.Errorf("encountered error '%s' while unmarshalling response: %s ", err, string(data)) |
| 42 | } | |
| 43 | resourceSets := autoGenerated.ResourceSets | |
| 44 | if len(resourceSets) == 0 { | |
| 45 | return 0, fmt.Errorf("no ResourceSets found in response: %s", string(data)) | |
| 46 | } | |
| 47 | resources := resourceSets[0].Resources | |
| 48 | if len(resources) == 0 { | |
| 49 | return 0, fmt.Errorf("no Resources found in response: %s", string(data)) | |
| 50 | } | |
| 51 | routeLegs := resources[0].RouteLegs | |
| 52 | if len(routeLegs) == 0 { | |
| 53 | return 0, fmt.Errorf("no RouteLegs found in response: %s", string(data)) | |
577ed62e 2023-08-27 Pavel Safronov | 54 | } |
6cc7663b 2023-08-28 Pavel Safronov | 55 | firstLeg := routeLegs[0] |
| 56 | dist := firstLeg.TravelDistance | |
577ed62e 2023-08-27 Pavel Safronov | 57 | return dist, nil |
| 58 | } | |
| 59 | ||
| 60 | // https://mholt.github.io/json-to-go/ | |
| 61 | type autoGenerated struct { | |
| 62 | AuthenticationResultCode string `json:"authenticationResultCode"` | |
| 63 | BrandLogoURI string `json:"brandLogoUri"` | |
| 64 | Copyright string `json:"copyright"` | |
| 65 | ResourceSets []struct { | |
| 66 | EstimatedTotal int `json:"estimatedTotal"` | |
| 67 | Resources []struct { | |
| 68 | Type string `json:"__type"` | |
| 69 | Bbox []float64 `json:"bbox"` | |
| 70 | ID string `json:"id"` | |
| 71 | DistanceUnit string `json:"distanceUnit"` | |
| 72 | DurationUnit string `json:"durationUnit"` | |
| 73 | RouteLegs []struct { | |
| 74 | ActualEnd struct { | |
| 75 | Type string `json:"type"` | |
| 76 | Coordinates []float64 `json:"coordinates"` | |
| 77 | } `json:"actualEnd"` | |
| 78 | ActualStart struct { | |
| 79 | Type string `json:"type"` | |
| 80 | Coordinates []float64 `json:"coordinates"` | |
| 81 | } `json:"actualStart"` | |
| 82 | AlternateVias []any `json:"alternateVias"` | |
| 83 | Description string `json:"description"` | |
| 84 | EndTime string `json:"endTime"` | |
| 85 | ItineraryItems []struct { | |
| 86 | CompassDirection string `json:"compassDirection"` | |
| 87 | Details []struct { | |
| 88 | CompassDegrees int `json:"compassDegrees"` | |
| 89 | EndPathIndices []int `json:"endPathIndices"` | |
| 90 | LocationCodes []string `json:"locationCodes"` | |
| 91 | ManeuverType string `json:"maneuverType"` | |
| 92 | Mode string `json:"mode"` | |
| 93 | Names []string `json:"names"` | |
| 94 | RoadType string `json:"roadType"` | |
| 95 | StartPathIndices []int `json:"startPathIndices"` | |
| 96 | } `json:"details"` | |
| 97 | Exit string `json:"exit"` | |
| 98 | IconType string `json:"iconType"` | |
| 99 | Instruction struct { | |
| 100 | FormattedText any `json:"formattedText"` | |
| 101 | ManeuverType string `json:"maneuverType"` | |
| 102 | Text string `json:"text"` | |
| 103 | } `json:"instruction"` | |
| 104 | IsRealTimeTransit bool `json:"isRealTimeTransit"` | |
| 105 | ManeuverPoint struct { | |
| 106 | Type string `json:"type"` | |
| 107 | Coordinates []float64 `json:"coordinates"` | |
| 108 | } `json:"maneuverPoint"` | |
| 109 | RealTimeTransitDelay int `json:"realTimeTransitDelay"` | |
| 110 | SideOfStreet string `json:"sideOfStreet"` | |
| 111 | TollZone string `json:"tollZone"` | |
| 112 | TransitTerminus string `json:"transitTerminus"` | |
| 113 | TravelDistance float64 `json:"travelDistance"` | |
| 114 | TravelDuration int `json:"travelDuration"` | |
| 115 | TravelMode string `json:"travelMode"` | |
| 116 | Hints []struct { | |
| 117 | HintType string `json:"hintType"` | |
| 118 | Text string `json:"text"` | |
| 119 | } `json:"hints,omitempty"` | |
| 120 | } `json:"itineraryItems"` | |
| 121 | RouteRegion string `json:"routeRegion"` | |
| 122 | RouteSubLegs []struct { | |
| 123 | EndWaypoint struct { | |
| 124 | Type string `json:"type"` | |
| 125 | Coordinates []float64 `json:"coordinates"` | |
| 126 | Description string `json:"description"` | |
| 127 | IsVia bool `json:"isVia"` | |
| 128 | LocationIdentifier string `json:"locationIdentifier"` | |
| 129 | RoutePathIndex int `json:"routePathIndex"` | |
| 130 | } `json:"endWaypoint"` | |
| 131 | StartWaypoint struct { | |
| 132 | Type string `json:"type"` | |
| 133 | Coordinates []float64 `json:"coordinates"` | |
| 134 | Description string `json:"description"` | |
| 135 | IsVia bool `json:"isVia"` | |
| 136 | LocationIdentifier string `json:"locationIdentifier"` | |
| 137 | RoutePathIndex int `json:"routePathIndex"` | |
| 138 | } `json:"startWaypoint"` | |
| 139 | TravelDistance float64 `json:"travelDistance"` | |
| 140 | TravelDuration int `json:"travelDuration"` | |
| 141 | } `json:"routeSubLegs"` | |
| 142 | StartTime string `json:"startTime"` | |
| 143 | TravelDistance float64 `json:"travelDistance"` | |
| 144 | TravelDuration int `json:"travelDuration"` | |
| 145 | TravelMode string `json:"travelMode"` | |
| 146 | } `json:"routeLegs"` | |
| 147 | TrafficCongestion string `json:"trafficCongestion"` | |
| 148 | TrafficDataUsed string `json:"trafficDataUsed"` | |
| 149 | TravelDistance float64 `json:"travelDistance"` | |
| 150 | TravelDuration int `json:"travelDuration"` | |
| 151 | TravelDurationTraffic int `json:"travelDurationTraffic"` | |
| 152 | TravelMode string `json:"travelMode"` | |
| 153 | } `json:"resources"` | |
| 154 | } `json:"resourceSets"` | |
| 155 | StatusCode int `json:"statusCode"` | |
| 156 | StatusDescription string `json:"statusDescription"` | |
| 157 | TraceID string `json:"traceId"` | |
| 158 | } |