Esempio n. 1
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);
        }
Esempio n. 2
0
        void ClearRoute(object sender = null, EventArgs e = null)
        {
            currRouteResult = null;

            Device.BeginInvokeOnMainThread(() =>
            {
                ClearMarkers();
                ClearPolylines();
            });
        }
Esempio 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));
                                }
                            });
                        }
                    }
                }
            }
        }