Esempio n. 1
0
        async void CalculateRoute()
        {
            Location userLocation = mapboxMap.MyLocation;

            if (userLocation == null)
            {
                System.Diagnostics.Debug.WriteLine("calculateRoute: User location is null, therefore, origin can't be set.");
                return;
            }

            Point origin = Point.FromLngLat(userLocation.Longitude, userLocation.Latitude);

            if (TurfMeasurement.Distance(origin, destination, TurfConstants.UnitMeters) < 50)
            {
                startRouteButton.Visibility = (ViewStates.Gone);
                return;
            }

            NavigationRoute.Builder navigationRouteBuilder = NavigationRoute.GetBuilder()
                                                             .AccessToken(Mapbox.AccessToken);

            navigationRouteBuilder.Origin(origin);
            navigationRouteBuilder.Destination(destination);
            if (waypoint != null)
            {
                navigationRouteBuilder.AddWaypoint(waypoint);
            }

            var directions = await navigationRouteBuilder.Build().GetRouteAsync();

            DirectionsRoute directionsRoute = directions.Routes()[0];

            route = directionsRoute;
            navigationMapRoute.AddRoutes(directions.Routes());
        }
        private void FindRouteWith(LocationEngineResult result)
        {
            Location userLocation = result.LastLocation;

            if (userLocation == null)
            {
                Timber.D("calculateRoute: User location is null, therefore, origin can't be set.");
                return;
            }
            Point origin = Point.FromLngLat(userLocation.Longitude, userLocation.Latitude);

            if (TurfMeasurement.Distance(origin, destination, TurfConstants.UnitMeters) < 50)
            {
                startRouteButton.Visibility = ViewStates.Gone;
                return;
            }

            NavigationRoute.Builder navigationRouteBuilder = NavigationRoute.InvokeBuilder(this)
                                                             .AccessToken(Mapbox.Mapboxsdk.Mapbox.AccessToken);
            navigationRouteBuilder.Origin(origin);
            navigationRouteBuilder.Destination(destination);
            if (waypoint != null)
            {
                navigationRouteBuilder.AddWaypoint(waypoint);
            }
            navigationRouteBuilder.EnableRefresh(true);
            navigationRouteBuilder.Build().GetRoute(new GetRouteCallback((response) =>
            {
                if (response != null && response.Routes().Any())
                {
                    route = response.Routes()[0];
                    navigationMapRoute.AddRoutes(response.Routes());
                    startRouteButton.Visibility = ViewStates.Visible;
                }
            }));
        }