Example #1
0
        private async void DrawRoute()
        {
            var to = await DataService.GetDriverOrder(_orderId);

            if (to == null)
            {
                return;
            }
            Device.BeginInvokeOnMainThread(() =>
            {
                if (CurrentPosition == null)
                {
                    return;
                }
                _route = new TKRoute
                {
                    TravelMode  = TKRouteTravelMode.Driving,
                    Source      = CurrentPosition,
                    Destination = new Xamarin.Forms.Maps.Position(to.Latitude, to.Longitude),
                    Color       = Color.Blue,
                    LineWidth   = 20,
                    Selectable  = false
                };
                Routes.Add(_route);
                Pins.Clear();
                _driverPin = new TKCustomMapPin
                {
                    Image    = Device.OnPlatform("car_icon.png", "car_icon.png", string.Empty),
                    Position = CurrentPosition
                };
                _toPin = new OrderPin
                {
                    Position        = new Xamarin.Forms.Maps.Position(to.Latitude, to.Longitude),
                    Id              = 1,
                    ShowCallout     = true,
                    DefaultPinColor = Color.Red
                };
                Pins.Add(_toPin);
                Pins.Add(_driverPin);
            });
        }
Example #2
0
        private async void DrawRoute()
        {
            var to      = DataService.GetDriverRoute();
            var drivers = await DataService.GetDriverDeliveryPosition();

            Device.BeginInvokeOnMainThread(() =>
            {
                List <DriverPin> avPins = new List <DriverPin>();
                var driver = drivers.FirstOrDefault();
                if (driver != null)
                //foreach (var driver in drivers)
                {
                    DriverRoute route = Routes.FirstOrDefault(x => x.DriverInfo.DriverId == driver.DriverId);
                    if (route == null)
                    {
                        route = new DriverRoute
                        {
                            TravelMode  = TKRouteTravelMode.Driving,
                            Source      = new Position(driver.Latitude, driver.Longitude),
                            Destination = new Position(to.Latitude, to.Longitude),
                            Color       = Color.Blue,
                            LineWidth   = 20,
                            Selectable  = false,
                            DriverInfo  = new Model.DriverInfo
                            {
                                DriverId = driver.DriverId
                            }
                        };
                        Routes.Add(route);
                    }
                    else
                    {
                        route.Source = new Position(driver.Latitude, driver.Longitude);
                    }
                    DriverPin driverPin = Pins.OfType <DriverPin>().FirstOrDefault(x => x.DriverInfo.DriverId == driver.DriverId);
                    if (driverPin == null)
                    {
                        driverPin = new DriverPin()
                        {
                            Image       = Device.OnPlatform("car_icon", "car_icon", string.Empty),
                            Position    = new Position(driver.Latitude, driver.Longitude),
                            DriverInfo  = driver,
                            ShowCallout = true,
                        };
                        Pins.Add(driverPin);
                    }
                    else
                    {
                        driverPin.Position = new Position(driver.Latitude, driver.Longitude);
                    }

                    var toPin = Pins.OfType <OrderPin>().FirstOrDefault(x => x.OrderInfo.Id == driver.DriverId);
                    if (toPin == null)
                    {
                        toPin = new OrderPin
                        {
                            Position        = new Position(to.Latitude, to.Longitude),
                            DefaultPinColor = Color.Red,
                            OrderInfo       = new Model.Order {
                                Id = driver.DriverId
                            }
                        };
                        Pins.Add(toPin);
                    }
                    else
                    {
                        toPin.Position = new Position(to.Latitude, to.Longitude);
                    }
                    avPins.Add(driverPin);
                }
                //clear not use pins
                var castPins = Pins.OfType <DriverPin>();
                for (int i = 0; i < castPins.Count(); i++)
                {
                    if (avPins.FirstOrDefault(x => x.DriverInfo.DriverId == castPins.ElementAt(i).DriverInfo.DriverId) == null)
                    {
                        Routes.Remove(Routes.First(x => x.DriverInfo.DriverId == castPins.ElementAt(i).DriverInfo.DriverId));
                        Pins.Remove(Pins.OfType <OrderPin>().First(x => x.OrderInfo.Id == castPins.ElementAt(i).DriverInfo.DriverId));
                        Pins.Remove(castPins.ElementAt(i));
                        i = 0;
                    }
                }
            });
        }