Example #1
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);
            }
        }
Example #2
0
        /// <summary>
        /// Raises <see cref="RouteCalculationFailed"/>
        /// </summary>
        /// <param name="error">The error</param>
        protected void OnRouteCalculationFailed(TKRouteCalculationError error)
        {
            this.RouteCalculationFailed?.Invoke(this, new TKGenericEventArgs <TKRouteCalculationError>(error));

            this.RaiseCommand(this.RouteCalculationFailedCommand, error);
        }
Example #3
0
 /// <inheritdoc/>
 void IMapFunctions.RaiseRouteCalculationFailed(TKRouteCalculationError route)
 {
     this.OnRouteCalculationFailed(route);
 }
Example #4
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);
            }
        }