private bool DistanceCheck(DirectionRootObject direction, Path path) { //If google apis can't find a charging station it defaults back to your geolocation //If the ditance between the start and the last waypoint is greater than the distance between //the start and the end stop looping. var currentRoute = direction.routes[direction.routes.Count - 1]; var firstLeg = currentRoute.legs[0]; var lastLeg = currentRoute.legs[currentRoute.legs.Count - 1]; var currentWaypoint = path.Waypoints[path.Waypoints.Count - 1]; var startLat = firstLeg.start_location.lat; var startLng = firstLeg.start_location.lng; var wayPointLat = currentWaypoint.Latitude; var wayPointLng = currentWaypoint.Longitude; var endLat = lastLeg.end_location.lat; var endLng = lastLeg.end_location.lng; var startToEnd = DistanceBetweenCoords(startLat, startLng, endLat, endLng, 'M'); var startToWaypoint = DistanceBetweenCoords(startLat, startLng, wayPointLat, wayPointLng, 'M'); //Multiplier just to be sure if (startToWaypoint > (startToEnd * 5)) { return(false); } return(true); }
private async Task <DirectionRootObject> Google_GetDirection(Path path) { DirectionRootObject rootObject = null; string request = GetRequestString(path); using (var client = new HttpClient()) { var responseTask = client.GetAsync(request); responseTask.Wait(); //To store result of web api response. var result = responseTask.Result; if (result.IsSuccessStatusCode) { var json = await result.Content.ReadAsStringAsync(); var settings = new JsonSerializerSettings(); try { rootObject = JsonConvert.DeserializeObject <DirectionRootObject>(json, settings); } catch (Exception e) { throw new Exception(e.Message); } } } return(rootObject); }
private async Task <bool> EvaluateDirection(DirectionRootObject direction, Path path) { try { if (path.IgnoreRange == true) { path.Arrived = true; return(false); } if (path.Waypoints.Count > 0 && DistanceCheck(direction, path) == false) { path.Arrived = false; return(false); } var stepList = new List <Step>(); var currentRoute = direction.routes[direction.routes.Count - 1]; var currentLeg = currentRoute.legs[currentRoute.legs.Count - 1]; foreach (var step in currentLeg.steps) { stepList.Add(step); if ((path.CurrentRangeM - step.distance.value) <= 0) { return(await FindChargingStationOnStep(stepList, path)); } else { path.CurrentRangeM -= step.distance.value; } } } catch (Exception e) { throw new Exception(e.Message); } path.Arrived = true; return(false); }