public void SortWaypointsTest()
 {
     string[] waypoints =
     {
         $"45.1%2C30.1",
         $"45.3%2C30.3",
         $"45.3%2C30.1"
     };
     foreach (string line in waypoints)
     {
         System.Console.WriteLine(line);
     }
     GoogleMapsAPI.SortWaypoints(waypoints);
     Assert.AreEqual("45.3%2C30.1", waypoints[1]);
 }
Beispiel #2
0
        //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();
                }
            }
        }