//InitPins() should be called before this method, _waypoints is added in InitPins() //A method that let's google handle the directions, shortest path etc. private async void MapDirections(string message = "") { //for diagnostic numberOfAPICalls++; Console.WriteLine($"{message} Number Of API Calls: #{numberOfAPICalls}"); map.Polylines.Clear(); if (_currentLocation == null) { _currentLocation = await Geolocation.GetLastKnownLocationAsync(); } Position lastKnownPosition = new Position(_currentLocation.Latitude, _currentLocation.Longitude); if (_waypoints.Count > 0 && App.CheckIfInternet()) { _invoicesCollection.Clear(); _direction = await GoogleMapsAPI.MapDirectionsWithWaypoints(lastKnownPosition, _waypoints.ToArray()); //Only create a line if it returns something from google if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { List <Position> directionPolylines = PolylineHelper.Decode(_direction.Routes[0].OverviewPolyline.Points).ToList(); CreatePolylinesOnMap(directionPolylines); foreach (int order in GoogleMapsAPI._waypointsOrder) { _invoicesCollection.Add(_invoices[order]); } DeliveryItemView.ItemsSource = _invoicesCollection; } else { await DisplayAlert("Oops", "Unable to map the directions, please try to use internet connections or restart the app", "OK"); } } else if (_waypoints.Count() == 0 && App.CheckIfInternet()) { _direction = await GoogleMapsAPI.MapDirectionsNoWaypoints(lastKnownPosition); if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { List <Position> directionPolylines = PolylineHelper.Decode(_direction.Routes[0].OverviewPolyline.Points).ToList(); CreatePolylinesOnMap(directionPolylines); } } }
//A method that let's google handle the directions, shortest path etc. private async void MapDirections(string message = "") { //for diagnostic numberOfAPICalls++; Console.WriteLine($"{message} Number Of API Calls: #{numberOfAPICalls}"); map.Polylines.Clear(); if (_currentLocation == null) { _currentLocation = await Geolocation.GetLastKnownLocationAsync(); } Position lastKnownPosition = new Position(_currentLocation.Latitude, _currentLocation.Longitude); // Position lastKnownPosition = new Position(-46.3, 168); if (_waypoints.Count > 0 && App.CheckIfInternet()) { _invoicesCollection.Clear(); if (_waypoints.Count > MAX_WAYPOINTS) { GoogleMapsAPI.SortWaypoints(lastKnownPosition, _waypoints.ToArray()); List <Invoice> tempInvoice = new List <Invoice>(); for (int i = 0; i < GoogleMapsAPI._waypointsOrder.Count; i++) { tempInvoice.Add(_invoices[GoogleMapsAPI._waypointsOrder[i]]); } _invoices = tempInvoice; } for (int i = 0; i < _waypoints.Count; i += MAX_WAYPOINTS) { string destinationWaypoint; int waypointCountLeft = _waypoints.Count - i; string[] orderedWaypoints; if (waypointCountLeft > MAX_WAYPOINTS) { orderedWaypoints = new string[MAX_WAYPOINTS]; _waypoints.CopyTo(i, orderedWaypoints, 0, MAX_WAYPOINTS); destinationWaypoint = orderedWaypoints[orderedWaypoints.Count() - 1]; } else { orderedWaypoints = new string[waypointCountLeft]; _waypoints.CopyTo(i, orderedWaypoints, 0, waypointCountLeft); if ((destinationWaypoint = Preferences.Get("EndPointGeoWaypoint", string.Empty)) == "") { destinationWaypoint = orderedWaypoints[orderedWaypoints.Count() - 1]; } } _direction = await GoogleMapsAPI.MapDirectionsWithWaypoints(lastKnownPosition, destinationWaypoint, orderedWaypoints); string[] newCurrentPosition = orderedWaypoints[orderedWaypoints.Count() - 1].Split(new string[] { "%2C" }, StringSplitOptions.None); lastKnownPosition = new Position(Convert.ToDouble(newCurrentPosition[0]), Convert.ToDouble(newCurrentPosition[1])); //Only create a line if it returns something from google if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { AddPolyLine(); foreach (int order in GoogleMapsAPI._waypointsOrder) { _invoicesCollection.Add(_invoices[order + i]); } } else { await DisplayAlert("Oops", "Unable to map the directions, Please make sure the address entered is legit", "OK"); } } DeliveryItemView.ItemsSource = _invoicesCollection; } else if (_waypoints.Count() == 0 && App.CheckIfInternet()) { string destinationPoint; if ((destinationPoint = Preferences.Get("EndPointGeoWaypoint", string.Empty)) == "") { return; } _direction = await GoogleMapsAPI.MapDirectionsNoWaypoints(lastKnownPosition, destinationPoint); if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { AddPolyLine(); } } }