示例#1
0
        /// <summary>
        /// Sets the route calculation data
        /// </summary>
        /// <param name="route">The PCL route</param>
        /// <param name="routeResult">The route api result</param>
        private void SetRouteData(TKRoute route, MapRoute routeResult)
        {
            var latLngBounds = routeResult.BoundingBox;

            var apiSteps       = routeResult.Legs.First().Maneuvers;
            var steps          = new TKRouteStep[apiSteps.Count];
            var routeFunctions = (IRouteFunctions)route;

            for (int i = 0; i < steps.Length; i++)
            {
                steps[i] = new TKRouteStep();
                var stepFunctions = (IRouteStepFunctions)steps[i];
                var apiStep       = apiSteps.ElementAt(i);

                stepFunctions.SetDistance(apiStep.LengthInMeters);
                stepFunctions.SetInstructions(apiStep.InstructionText);
            }
            routeFunctions.SetSteps(steps);
            routeFunctions.SetDistance(routeResult.Legs.First().LengthInMeters);
            routeFunctions.SetTravelTime(routeResult.Legs.First().EstimatedDuration.TotalMinutes);

            routeFunctions.SetBounds(
                MapSpan.FromCenterAndRadius(
                    latLngBounds.Center.ToPosition(),
                    Distance.FromKilometers(
                        new Position(latLngBounds.SoutheastCorner.Latitude, latLngBounds.SoutheastCorner.Longitude)
                        .DistanceTo(
                            new Position(latLngBounds.NorthwestCorner.Latitude, latLngBounds.NorthwestCorner.Longitude)) / 2)));

            routeFunctions.SetIsCalculated(true);
        }
示例#2
0
        /// <summary>
        /// Sets the route calculation data
        /// </summary>
        /// <param name="route">The PCL route</param>
        /// <param name="routeResult">The rourte api result</param>
        private void SetRouteData(TKRoute route, GmsRouteResult routeResult)
        {
            var latLngBounds = new LatLngBounds(
                new LatLng(routeResult.Bounds.SouthWest.Latitude, routeResult.Bounds.SouthWest.Longitude),
                new LatLng(routeResult.Bounds.NorthEast.Latitude, routeResult.Bounds.NorthEast.Longitude));

            var apiSteps       = routeResult.Legs.First().Steps;
            var steps          = new TKRouteStep[apiSteps.Count()];
            var routeFunctions = (IRouteFunctions)route;


            for (int i = 0; i < steps.Length; i++)
            {
                steps[i] = new TKRouteStep();
                var stepFunctions = (IRouteStepFunctions)steps[i];
                var apiStep       = apiSteps.ElementAt(i);

                stepFunctions.SetDistance(apiStep.Distance.Value);
                stepFunctions.SetInstructions(apiStep.HtmlInstructions);
            }
            routeFunctions.SetSteps(steps);
            routeFunctions.SetDistance(routeResult.Legs.First().Distance.Value);
            routeFunctions.SetTravelTime(routeResult.Legs.First().Duration.Value);

            routeFunctions.SetBounds(
                MapSpan.FromCenterAndRadius(
                    latLngBounds.Center.ToPosition(),
                    Distance.FromKilometers(
                        new Position(latLngBounds.Southwest.Latitude, latLngBounds.Southwest.Longitude)
                        .DistanceTo(
                            new Position(latLngBounds.Northeast.Latitude, latLngBounds.Northeast.Longitude)) / 2)));
            routeFunctions.SetIsCalculated(true);
        }
 public HtmlInstructionsViewModel(TKRoute route)
 {
     Instructions      = new HtmlWebViewSource();
     Instructions.Html = @"<html><body>";
     foreach (var s in route.Steps)
     {
         Instructions.Html += string.Format("<b>{0}km:</b> {1}<br /><hr />", s.Distance / 1000, s.Instructions);
     }
     Instructions.Html += @"</body></html>";
 }
示例#4
0
 public HtmlInstructionsPageModel(TKRoute route)
 {
     Instructions = new HtmlWebViewSource {
         Html = @"<html><body>"
     };
     foreach (var s in route.Steps)
     {
         Instructions.Html += $"<b>{s.Distance/1000}km:</b> {s.Instructions}<br /><hr />";
     }
     Instructions.Html += @"</body></html>";
 }
示例#5
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);
            });
        }
        private void SetMyLocation()
        {
            Pins.Clear();
            routes.Clear();
            var route = new TKRoute();

            route.TravelMode = TKRouteTravelMode.Driving;
            var myposition = new Position(Settings.CarLat, Settings.CarLng);
            var toposition = new Position(Convert.ToDouble(Settings.Latfrom), Convert.ToDouble(Settings.Lngfrom));

            route.Source      = myposition;
            route.Destination = toposition;
            route.Color       = Color.DarkBlue;
            route.LineWidth   = 4;

            Pins.Add(new RoutePin
            {
                Route       = route,
                Image       = "deliverytruck.png",
                IsSource    = true,
                IsDraggable = false,
                Position    = myposition,
                Title       = "From",
                ShowCallout = true,
            });
            Pins.Add(new RoutePin
            {
                Route           = route,
                IsSource        = false,
                IsDraggable     = false,
                Image           = "placeholder.png",
                Position        = toposition,
                Title           = "To",
                ShowCallout     = true,
                DefaultPinColor = Color.Blue
            });
            routes.Add(route);
            OrderMap.Routes = routes;
            OrderMap.Pins   = Pins;
        }
        private async void SetMyLocation()
        {
            Pins.Clear();
            routes.Clear();
            var route = new TKRoute();

            route.TravelMode = TKRouteTravelMode.Driving;
            var myposition = new Position(Convert.ToDouble(Settings.LastLat), Convert.ToDouble(Settings.LastLng));
            var toposition = new Position(Convert.ToDouble(Settings.Latto), Convert.ToDouble(Settings.Lngto));

            route.Source      = myposition;
            route.Destination = toposition;
            route.Color       = Color.OrangeRed;
            route.LineWidth   = 4;

            Pins.Add(new RoutePin
            {
                Route       = route,
                Image       = "deliverytruck.png",
                IsSource    = true,
                IsDraggable = true,
                Position    = myposition,
                Title       = "From",
                ShowCallout = true,
            });
            Pins.Add(new RoutePin
            {
                Route           = route,
                IsSource        = false,
                IsDraggable     = true,
                Position        = toposition,
                Title           = "To",
                ShowCallout     = true,
                DefaultPinColor = Color.Red
            });
            routes.Add(route);
            OrderMap.Routes = routes;
            OrderMap.Pins   = Pins;
        }
示例#8
0
        public async void itemselected()
        {
            switch (TapItem)
            {
            case "Llamar":
                var cliente = ListaClientes.Where(x => x.NombreCliente == MyPin.Title).FirstOrDefault();

                var PhoneCallTask = CrossMessaging.Current.PhoneDialer;
                if (PhoneCallTask.CanMakePhoneCall)
                {
                    PhoneCallTask.MakePhoneCall(cliente.Telefono, cliente.NombreCliente);
                }
                break;

            case "Vender":
                App.clienteseleccionado = ListaClientes.Where(x => x.NombreCliente == MyPin.Title).FirstOrDefault();
                await App.Navigator.PushAsync(new DetallePage());

                break;

            case "Ruta":
                Routes.Clear();
                TKRoute route = new TKRoute
                {
                    TravelMode  = TKRouteTravelMode.Driving,
                    Source      = CenterSearch.Center,
                    Destination = MyPin.Position,
                    Color       = Color.Blue,
                    LineWidth   = 5
                };

                Routes.Add(route);
                Debug.WriteLine(route.Distance);
                Routes.Count();
                break;
            }
        }
        //private async void OnNotificationOpened(OSNotificationOpenedResult result)
        //{
        //    if (result.notification?.payload?.additionalData == null)
        //    {
        //        return;
        //    }

        //    if (result.notification.payload.additionalData.ContainsKey("body"))
        //    {
        //        var labelText = result.notification.payload.additionalData["body"].ToString();
        //        Settings.LastNotify = labelText;
        //        await DisplayAlert(AppResources.Error, Settings.LastNotify, AppResources.Ok);
        //    }

        //}

        //private void OnNotificationRecevied(OSNotification notification)
        //{

        //    if (notification.payload?.additionalData == null)
        //    {
        //        return;
        //    }

        //    if (notification.payload.additionalData.ContainsKey("body"))
        //    {
        //        var title = notification.payload.additionalData["title"].ToString();
        //        if (title != "The trip is completed")
        //        {
        //            var labelText = notification.payload.additionalData["body"].ToString();
        //            var Req = JsonConvert.DeserializeObject<DelivaryObject>(labelText);
        //            Settings.CarLat = Req.Position.Latitude;
        //            Settings.CarLng = Req.Position.Longitude;

        //            SetMyLocation();
        //        }
        //        else
        //        {
        //           App.Current.MainPage = new HomePage();
        //            DisplayAlert(AppResources.Alert, AppResources.TripCompleted, AppResources.Ok);

        //            //Device.BeginInvokeOnMainThread(() =>
        //            //{
        //            //    Navigation.PushModalAsync(new HomePage());
        //            //});

        //        }

        //    }
        //}
        private void SetMyLocation()
        {
            //    MoveMapToMyLocation();
            var route = new TKRoute();

            route.TravelMode = TKRouteTravelMode.Driving;
            var myposition = new Position(31.209523, 31.491892);
            var toposition = new Position(Convert.ToDouble(Settings.Latfrom), Convert.ToDouble(Settings.Lngfrom));

            route.Source      = myposition;
            route.Destination = toposition;
            route.Color       = Color.DodgerBlue;
            route.LineWidth   = 25;
            Pins.Add(new RoutePin
            {
                Route           = route,
                IsSource        = true,
                IsDraggable     = false,
                Position        = myposition,
                Title           = "From",
                ShowCallout     = true,
                DefaultPinColor = Color.Green
            });
            Pins.Add(new RoutePin
            {
                Route           = route,
                IsSource        = true,
                IsDraggable     = false,
                Position        = toposition,
                Title           = "To",
                ShowCallout     = true,
                DefaultPinColor = Color.Red
            });
            routes.Add(route);
            OrderMap.Routes = routes;
            OrderMap.Pins   = Pins;
        }
 /// <summary>
 /// Creates a new instance of <see cref="TKRouteCalculationError"/>
 /// </summary>
 /// <param name="route">The route</param>
 /// <param name="errorMessage">The error message</param>
 public TKRouteCalculationError(TKRoute route, string errorMessage)
 {
     Route        = route;
     ErrorMessage = errorMessage;
 }
示例#11
0
        /// <summary>
        /// Calculates and adds the route to the map
        /// </summary>
        /// <param name="route">The route to add</param>
        private async void AddRoute(TKRoute route)
        {
            if (route == null)
            {
                return;
            }

            _tempRouteList.Add(route);

            route.PropertyChanged += OnRoutePropertyChanged;

            MapRouteFinderResult routeData;
            string errorMessage = null;

            switch (route.TravelMode)
            {
            case TKRouteTravelMode.Walking:
                routeData = await MapRouteFinder.GetWalkingRouteAsync(
                    route.Source.ToLocationCoordinate(), route.Destination.ToLocationCoordinate());

                break;

            default:
                routeData = await MapRouteFinder.GetDrivingRouteAsync(
                    route.Source.ToLocationCoordinate(), route.Destination.ToLocationCoordinate(),
                    MapRouteOptimization.Time, MapRouteRestrictions.None);

                break;
            }

            if (FormsMap == null || Map == null || !_tempRouteList.Contains(route))
            {
                return;
            }

            if (routeData != null && routeData.Route != null)
            {
                if (routeData.Status == MapRouteFinderStatus.Success)
                {
                    var r = routeData.Route;
                    if (r != null && r.Path.Positions != null && r.Path.Positions.Any())
                    {
                        SetRouteData(route, r);

                        var polyline = new MapPolyline();

                        if (route.Color != Color.Default)
                        {
                            polyline.StrokeColor = route.Color.ToUWPColor();
                        }
                        if (route.LineWidth > 0)
                        {
                            polyline.StrokeThickness = route.LineWidth;
                        }

                        if (r.Path != null)
                        {
                            polyline.Path = new Geopath(r.Path.Positions.Select(i => new BasicGeoposition
                            {
                                Latitude  = i.Latitude,
                                Longitude = i.Longitude
                            }));
                        }

                        Map.MapElements.Add(polyline);
                        _routes.Add(route, polyline);

                        MapFunctions.RaiseRouteCalculationFinished(route);
                    }
                    else
                    {
                        errorMessage = "Unexpected result";
                    }
                }
                else
                {
                    errorMessage = routeData.Status.ToString();
                }
            }
            else
            {
                errorMessage = "Could not connect to api";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                var routeCalculationError = new TKRouteCalculationError(route, errorMessage);

                MapFunctions.RaiseRouteCalculationFailed(routeCalculationError);
            }
        }
示例#12
0
        /// <summary>
        /// Raises <see cref="RouteClicked"/>
        /// </summary>
        /// <param name="route">The tapped route</param>
        protected void OnRouteClicked(TKRoute route)
        {
            RouteClicked?.Invoke(this, new TKGenericEventArgs <TKRoute>(route));

            RaiseCommand(RouteClickedCommand, route);
        }
示例#13
0
 /// <inheritdoc/>
 void IMapFunctions.RaiseRouteCalculationFinished(TKRoute route)
 {
     this.OnRouteCalculationFinished(route);
 }
示例#14
0
 /// <inheritdoc/>
 void IMapFunctions.RaiseRouteClicked(TKRoute route)
 {
     this.OnRouteClicked(route);
 }
示例#15
0
        /// <summary>
        /// Raises <see cref="RouteCalculationFinished"/>
        /// </summary>
        /// <param name="route">The route</param>
        protected void OnRouteCalculationFinished(TKRoute route)
        {
            this.RouteCalculationFinished?.Invoke(this, new TKGenericEventArgs <TKRoute>(route));

            this.RaiseCommand(this.RouteCalculationFinishedCommand, route);
        }
示例#16
0
 /// <inheritdoc/>
 void IMapFunctions.RaiseRouteClicked(TKRoute route) => OnRouteClicked(route);
示例#17
0
        public HtmlInstructionsPage(TKRoute route)
        {
            InitializeComponent();

            this.BindingContext = new HtmlInstructionsViewModel(route);
        }
示例#18
0
        /// <summary>
        /// Calculates and adds the route to the map
        /// </summary>
        /// <param name="route">The route to add</param>
        private async void AddRoute(TKRoute route)
        {
            if (route == null)
            {
                return;
            }

            this._tempRouteList.Add(route);

            route.PropertyChanged += OnRoutePropertyChanged;

            GmsDirectionResult routeData    = null;
            string             errorMessage = null;

            routeData = await GmsDirection.Instance.CalculateRoute(route.Source, route.Destination, route.TravelMode.ToGmsTravelMode());

            if (this.FormsMap == null || this.Map == null || !this._tempRouteList.Contains(route))
            {
                return;
            }

            if (routeData != null && routeData.Routes != null)
            {
                if (routeData.Status == GmsDirectionResultStatus.Ok)
                {
                    var r = routeData.Routes.FirstOrDefault();
                    if (r != null && r.Polyline.Positions != null && r.Polyline.Positions.Any())
                    {
                        this.SetRouteData(route, r);

                        var routeOptions = new PolylineOptions();

                        if (route.Color != Color.Default)
                        {
                            routeOptions.InvokeColor(route.Color.ToAndroid().ToArgb());
                        }
                        if (route.LineWidth > 0)
                        {
                            routeOptions.InvokeWidth(route.LineWidth);
                        }
                        routeOptions.Add(r.Polyline.Positions.Select(i => i.ToLatLng()).ToArray());

                        this._routes.Add(route, this._googleMap.AddPolyline(routeOptions));

                        this.MapFunctions.RaiseRouteCalculationFinished(route);
                    }
                    else
                    {
                        errorMessage = "Unexpected result";
                    }
                }
                else
                {
                    errorMessage = routeData.Status.ToString();
                }
            }
            else
            {
                errorMessage = "Could not connect to api";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                var routeCalculationError = new TKRouteCalculationError(route, errorMessage);

                this.MapFunctions.RaiseRouteCalculationFailed(routeCalculationError);
            }
        }