Exemplo n.º 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;
            }

            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);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Getroute/Draw route between two given position.
        /// </summary>
        public async void Getroute()
        {
            GmsDirectionResult Routeresult = await GmsDirection.
                                             Instance.CalculateRoute(new Position(17.4474, 78.3762),
                                                                     new Position(17.4375, 78.4483), GmsDirectionTravelMode.Driving, null);

            var routes = Routeresult.Routes.FirstOrDefault();

            Lines = new ObservableCollection <TKPolyline>();
            Pins  = new ObservableCollection <TKCustomMapPin>();
            IEnumerable <Position> pos = routes.Polyline.Positions;

            var line = new TKPolyline
            {
                Color           = Color.Green,
                LineWidth       = 10f,
                LineCoordinates = pos.ToList()
            };

            Pins.Add(new TKCustomMapPin
            {
                //Route = route,
                //IsSource = true,
                IsDraggable     = true,
                Position        = new Position(17.4474, 78.3762),
                ShowCallout     = true,
                DefaultPinColor = Color.Green
            });
            Pins.Add(new TKCustomMapPin
            {
                //Route = route,
                //IsSource = false,
                IsDraggable     = true,
                Position        = new Position(17.4375, 78.4483),
                ShowCallout     = true,
                DefaultPinColor = Color.Red
            });

            Lines.Add(line);



            //var route = new TKRoute
            //{
            //    TravelMode = TKRouteTravelMode.Driving,
            //    Source = new Position(17.4474, 78.3762),
            //    Destination = new Position(17.4375,78.4483),
            //    Color = Color.Red,
            //    LineWidth = 5
            //};
            //Routes = new ObservableCollection<TKRoute>();
            //Pins = new ObservableCollection<TKCustomMapPin>();
            //Pins.Add(new RoutePin
            //{
            //    Route = route,
            //    IsSource = true,
            //    IsDraggable = true,
            //    Position = new Position(17.4474, 78.3762),
            //    ShowCallout = true,
            //    DefaultPinColor = Color.Green
            //});
            //Pins.Add(new RoutePin
            //{
            //    Route = route,
            //    IsSource = false,
            //    IsDraggable = true,
            //    Position = new Position(17.4375, 78.4483),
            //    ShowCallout = true,
            //    DefaultPinColor = Color.Red
            //});

            //Routes.Add(route);
        }
Exemplo n.º 3
0
        async void CalculateRouteDetailsAysnc(LatLng sourceLatLng, LatLng destLatLng, bool markSource, bool markDest)
        {
            string strGoogleDirectionUrl = BuildGoogleDirectionUrl(sourceLatLng, destLatLng);

            string strJSONDirectionResponse = await HttpRequest(strGoogleDirectionUrl);

            if (strJSONDirectionResponse != "error")
            {
                if (markSource)
                {
                    AddMarker("Source", sourceLatLng);
                }

                if (markDest)
                {
                    AddMarker("Destination", destLatLng);
                }
            }

            GmsDirectionResult routeData = JsonConvert.DeserializeObject <GmsDirectionResult>(strJSONDirectionResponse);

            if (routeData != null && routeData.Routes != null)
            {
                if (routeData.Status == GmsDirectionResultStatus.Ok)
                {
                    GmsRouteResult routeResult;
                    TimeSpan       timespan;

                    for (int i = routeData.Routes.Count() - 1; i >= 0; i--)
                    {
                        routeResult = routeData.Routes.ElementAt(i);

                        timespan = routeResult.Duration();
                        System.Diagnostics.Debug.WriteLine(timespan.ToString(@"hh\:mm\:ss\:fff"));

                        if (routeResult != null && routeResult.Polyline.Positions != null && routeResult.Polyline.Positions.Count() > 0)
                        {
                            PolylineOptions polylineOptions = new PolylineOptions();

                            if (i == 0)
                            {
                                currRouteResult = routeResult;
                                polylineOptions.InvokeColor(Android.Graphics.Color.Blue.ToArgb());
                            }
                            else
                            {
                                polylineOptions.InvokeColor(Android.Graphics.Color.Gray.ToArgb());
                            }

                            foreach (Position position in routeResult.Polyline.Positions)
                            {
                                polylineOptions.Add(new LatLng(position.Latitude, position.Longitude));
                            }

                            Device.BeginInvokeOnMainThread(() =>
                            {
                                polylineList.Add(googleMap.AddPolyline(polylineOptions));

                                if (routeResult == currRouteResult)
                                {
                                    LatLng sw = new LatLng(routeResult.Bounds.SouthWest.Latitude, routeResult.Bounds.SouthWest.Longitude);
                                    LatLng ne = new LatLng(routeResult.Bounds.NorthEast.Latitude, routeResult.Bounds.NorthEast.Longitude);

                                    googleMap.AnimateCamera(CameraUpdateFactory.NewLatLngBounds(new LatLngBounds(sw, ne), 120));
                                }
                            });
                        }
                    }
                }
            }
        }