public TravelData getOriginalRoute(string travelMode, GeoPoint originXY, GeoPoint destinationXY) { TravelData travelData = new TravelData(); travelData.steps = new List <Step> (); // ORIGIN -> PLACE DATA string url = @"https://maps.googleapis.com/maps/api/directions/json?mode=" + travelMode + "&origin=" + originXY.latitude + "," + originXY.longitude + "&destination=" + destinationXY.latitude + "," + destinationXY.longitude + "&key="; var httpClient = new HttpClient(new NativeMessageHandler()); Task <HttpResponseMessage> response = httpClient.GetAsync(url); Task <string> returnedText = response.Result.Content.ReadAsStringAsync(); var json = (JObject)JsonConvert.DeserializeObject(returnedText.Result); string status = (string)json["status"]; if (status == "OK") { try { var routes = json["routes"]; var route = routes[0]; var legs = route["legs"]; var leg = legs[0]; var totalDistance = leg["distance"]; travelData.meters = (int)totalDistance["value"]; var totalDuration = leg["duration"]; travelData.seconds = (int)totalDuration["value"]; var steps = leg["steps"]; foreach (JToken step in steps) { Step nextStep = new Step(); JToken startLoc = step["start_location"]; nextStep.originXY = new GeoPoint((double)startLoc["lat"], (double)startLoc["lng"]); JToken endLoc = step["end_location"]; nextStep.endpointXY = new GeoPoint((double)endLoc["lat"], (double)endLoc["lng"]); JToken polyLine = step["polyline"]; nextStep.polyLine = (string)polyLine["points"]; JToken distance = step["distance"]; nextStep.distance = (int)distance["value"]; travelData.steps.Add(nextStep); } } catch (Exception ex) { } } else { //error handling } return(travelData); }
public List <GeoPoint> findPointsToSearch(TravelData originalRoute, int searchRadius, GeoPoint originXY) { // START OFF BY ADDING THE ORIGIN POINT List <GeoPoint> pointsToSearch = new List <GeoPoint>(); pointsToSearch.Add(originXY); // CONTINUE SEARCHING ALONG THE NEXT STEP UNTIL FOUND ALL POINTS for (int i = 0; i < originalRoute.steps.Count; i++) { List <GeoPoint> morePoints = findPointsAlongStep(originalRoute.steps[i], searchRadius); foreach (GeoPoint point in morePoints) { pointsToSearch.Add(point); } } return(pointsToSearch); }
public async Task <List <Place> > Run(GeoPoint startCoordinate, GeoPoint endCoordinate) // I think pass the coordinates as params because startCoord will probably be pulled // from the native phones GPS //public List<Place> Run(GeoPoint startCoordinate, GeoPoint endCoordinate) { DateTime timeStarted = DateTime.Now; //GET TRAVEL DATA FOR THE ORIGINAL ROUTE GeoPoint originXY = startCoordinate; //getCurrentLocation (); GeoPoint finalDestinationXY = endCoordinate; //getFinalDestination (); string placeType = "thai food"; string transportationMethod = "driving"; int searchRadius = 100; TravelData originalRoute = getOriginalRoute(transportationMethod, originXY, finalDestinationXY); decimal originalMinutes = Math.Round((originalRoute.seconds / 60.0m), 0); List <GeoPoint> pointsToSearch = findPointsToSearch(originalRoute, searchRadius, originXY); // foreach (GeoPoint point in pointsToSearch) // { // Logger.info ("SearchPoint", point.latitude + "," + point.longitude); // } List <List <GeoPoint> > pointsArrays = new List <List <GeoPoint> >(); List <GeoPoint> pointsArray1 = new List <GeoPoint>(); List <GeoPoint> pointsArray2 = new List <GeoPoint>(); List <GeoPoint> pointsArray3 = new List <GeoPoint>(); List <GeoPoint> pointsArray4 = new List <GeoPoint>(); List <GeoPoint> pointsArray5 = new List <GeoPoint>(); List <GeoPoint> pointsArray6 = new List <GeoPoint>(); List <GeoPoint> pointsArray7 = new List <GeoPoint>(); List <GeoPoint> pointsArray8 = new List <GeoPoint>(); pointsArrays.Add(pointsArray1); pointsArrays.Add(pointsArray2); pointsArrays.Add(pointsArray3); pointsArrays.Add(pointsArray4); pointsArrays.Add(pointsArray5); pointsArrays.Add(pointsArray6); pointsArrays.Add(pointsArray7); pointsArrays.Add(pointsArray8); int numberPointsPerTask = pointsToSearch.Count / pointsArrays.Count; numberPointsPerTask++; int numPointsAdded = 0; int pointsArraysIndex = 0; for (int i = 0; i < pointsToSearch.Count; i++) { if (numPointsAdded == numberPointsPerTask) { numPointsAdded = 0; pointsArraysIndex++; } pointsArrays[pointsArraysIndex].Add(pointsToSearch[i]); numPointsAdded++; } // List<List<Place>> allPlaces = new List<List<Place>> (); // List<Place> places1 = await searchPlacesFromListPoints (pointsArray1, searchRadius, placeType); // List<Place> places2 = await searchPlacesFromListPoints (pointsArray2, searchRadius, placeType); // List<Place> places3 = await searchPlacesFromListPoints (pointsArray3, searchRadius, placeType); // List<Place> places4 = await searchPlacesFromListPoints (pointsArray4, searchRadius, placeType); // allPlaces.Add (places1); // allPlaces.Add (places2); // allPlaces.Add (places3); // allPlaces.Add (places4); Task <List <Place> >[] allPlaces = { Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray1, searchRadius, placeType)), Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray2, searchRadius, placeType)), Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray3, searchRadius, placeType)), Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray4, searchRadius, placeType)), Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray5, searchRadius, placeType)), Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray6, searchRadius, placeType)), Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray7, searchRadius, placeType)), Task <List <Place> > .Run(() => runTasksSearchPlaces(pointsArray8, searchRadius, placeType)) }; Task.WaitAll(allPlaces); List <Place> placesToSearch = new List <Place>(); for (int x = 0; x < allPlaces.Length; x++) { foreach (Place place in allPlaces[x].Result) { placesToSearch.Add(place); } } placesToSearch = removeDuplicatePlaces(placesToSearch); int index = 0; List <Place> placesToSearch2 = new List <Place> (); foreach (Place place in placesToSearch) { placesToSearch2.Add(getPlaceDetails(place.id)); } TimeSpan duration = DateTime.Now - timeStarted; Logger.info("Results", "Number Of Places Found: " + placesToSearch2.Count); Logger.info("Results", "Place Searches Calls: " + numTimesSearchPlacesCalled); Logger.info("Results", "Place Details Calls: " + placesToSearch2.Count); Logger.info("Results", "Total API Calls: " + (placesToSearch2.Count + numTimesSearchPlacesCalled)); Logger.info("Results", "Total Time: " + duration.ToString()); foreach (Place place in placesToSearch2) { Logger.info("Results", " "); Logger.info("Results", place.Name); Logger.info("Results", "Open now: " + place.openNow); Logger.info("Results", "Open: " + place.openTime); Logger.info("Results", "Close: " + place.CloseTime); Logger.info("Results", "Coordinates: " + place.Location.latitude + "," + place.Location.longitude); } foreach (Place place in placesToSearch2) { if (place.openNow) { place.OpenColor = Color.Green; } else { place.OpenColor = Color.Red; } TravelData travelData = getPitStopRoute(transportationMethod, originXY, place.Location, finalDestinationXY); int pitStopAddedSeconds = travelData.seconds - originalRoute.seconds; place.SecondsAdded = pitStopAddedSeconds; if (pitStopAddedSeconds > 60) { int minsAdded = pitStopAddedSeconds / 60; place.AddedTime = minsAdded + " Mins"; } else { place.AddedTime = pitStopAddedSeconds + " Seconds"; } } return(placesToSearch2); }