Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DirectionsService.Route method return a DirectionsResult #108

Merged
merged 1 commit into from
Feb 13, 2021
Merged

Make DirectionsService.Route method return a DirectionsResult #108

merged 1 commit into from
Feb 13, 2021

Conversation

NickBarrett
Copy link
Contributor

Hi there,

We are wanting to have DirectionsService.Route return a DirectionsResult. We have been (and still do) use DirectionsRenderer.Route to do this, but we wanted to be able to query the Api without displaying the results on the map.

We investigated making HTTP calls directly to the Directions Api but Google blocks this (through CORS) when you are calling from a browser (as in Blazor WebAssembly) so the only option is the use the Javascript Api.

The original DirectionsService.Route returned a DirectionResponse but on inspecting the code it was always null.

To solve the issue we copied the code from the DirectionsRenderer.Route method. We also had to change objectManager.js to ensure that the setDirections method was only called if it existed. setDirections does exist in DirectionsRenderer but not in DirectionsService.

We realise changing the return type of DirectionsService.Route to a DirectionResponse is a breaking change but, without wanting to offend, the original code would always return null anyway, so we don't believe this would be an issue for anyone using the library already.

To test the result of this PR, replacing the code in MapRoutes.razor.cs in the demo with the following code will get the route result and update the duration and distance but not draw the route on the map.

        private DirectionsService dirService;

        private async Task OnAfterInitAsync()
        {
            //Create instance of DirectionRenderer
            dirRend = await DirectionsRenderer.CreateAsync(map1.JsRuntime, new DirectionsRendererOptions()
            {
                Map = map1.InteropObject
            });

            dirService = await DirectionsService.CreateAsync(map1.JsRuntime);
        }

        private async Task AddDirections()
        {
            //Adding a waypoint
            var waypoints = new List<DirectionsWaypoint>();
            waypoints.Add(new DirectionsWaypoint() { Location = "Bethlehem, PA", Stopover = true });

            //Direction Request
            DirectionsRequest dr = new DirectionsRequest();
            dr.Origin = "Allentown, PA";
            dr.Destination = "Bronx, NY";
            dr.Waypoints = waypoints;
            dr.TravelMode = TravelMode.Driving;
            dr.DrivingOptions = new DrivingOptions()
            {
                DepartureTime = DateTime.Now.AddHours(1)
            };

            //Calculate Route
            _directionsResult = await dirService.Route(dr, new DirectionsRequestOptions()
            {
                StripLegsStepsLatLngs = false,
                StripOverviewPath = false,
                StripOverviewPolyline = false,
                StripLegsStepsPath = false,
                StripLegsSteps = false
            });


            var routes = _directionsResult.Routes.SelectMany(x => x.Legs).ToList();

            foreach (var route in routes)
            {
                _durationTotalString += route.DurationInTraffic?.Text;
                _distanceTotalString += route.Distance.Text;
            }
        }

…lt. Code was copied from DirectionsRenderer's Route method as we believe these should both perform the function with the difference that a DirectionsRenderer also displays the result on the map and DirectionsService does not.

The change to objectManager.js ensures that the setDirections method is only called if it exists. setDirections does exist in DirectionsRenderer but not in DirectionsService.
@valentasm1
Copy link
Collaborator

And real world use case? Fix me if i am wrong, since i didnt go thourgh carefull so far
You have entry somewhere outside the map. User enters some location in that text box. You match it do some your processing and display on map if success?

@NickBarrett
Copy link
Contributor Author

In our case our user enters locations and generate routes between them using DirectionsRenderer and display them on the map. In the background though we want to use a DirectionsService to work out the distance and duration of routes between our own predefined set of locations and the user's entered location. In our scenario this information is of little use to the user so we don't want to display it at all.

@valentasm1 valentasm1 merged commit 8b5296f into rungwiroon:master Feb 13, 2021
@valentasm1
Copy link
Collaborator

If one day all will be reverted it mean i got a lot of complains :). Just check on release log to see what happen
https://www.nuget.org/packages/BlazorGoogleMaps/1.0.12

@NickBarrett
Copy link
Contributor Author

Thanks for accepting the PR and thanks, as always, for working on and maintaining this project.
We always keep an eye on what's going on with the project to look out for breaking changes, as well as new developments that we could take advantage of, so no worries if you have to revert the code 👍

@valentasm1
Copy link
Collaborator

No problem
Thank you for making PR, testing and spending time to explain clearly changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants