Skip to content

Commit

Permalink
DirectionsService.Route is using old javascript function name #243
Browse files Browse the repository at this point in the history
  • Loading branch information
valentasm committed Mar 13, 2023
1 parent 57cc2c5 commit e0301cb
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 82 deletions.
2 changes: 1 addition & 1 deletion GoogleMapsComponents/GoogleMapsComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<RazorLangVersion>3.0</RazorLangVersion>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<PackageId>BlazorGoogleMaps</PackageId>
<Version>2.5.5</Version>
<Version>2.5.6</Version>
<Authors>Rungwiroon</Authors>
<Company>QueueStack Solution</Company>
<Product>BlazorGoogleMaps</Product>
Expand Down
8 changes: 4 additions & 4 deletions GoogleMapsComponents/Maps/DirectionsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DirectionsService : IDisposable
/// <summary>
/// Creates a new instance of a DirectionsService that sends directions queries to Google servers.
/// </summary>
public async static Task<DirectionsService> CreateAsync(IJSRuntime jsRuntime)
public static async Task<DirectionsService> CreateAsync(IJSRuntime jsRuntime)
{
var jsObjectRef = await JsObjectRef.CreateAsync(jsRuntime, "google.maps.DirectionsService");

Expand Down Expand Up @@ -43,16 +43,17 @@ public void Dispose()
/// <param name="request"></param>
/// <param name="directionsRequestOptions">Lets you specify which route response paths to opt out from clearing.</param>
/// <returns></returns>
public async Task<DirectionsResult> Route(DirectionsRequest request, DirectionsRequestOptions directionsRequestOptions = null)
public async Task<DirectionsResult?> Route(DirectionsRequest request, DirectionsRequestOptions? directionsRequestOptions = null)
{
if (directionsRequestOptions == null)
{
directionsRequestOptions = new DirectionsRequestOptions();
}

var response = await _jsObjectRef.InvokeAsync<string>(
"googleMapDirectionServiceFunctions.route",
"blazorGoogleMaps.directionService.route",
request, directionsRequestOptions);

try
{
var dirResult = JsonConvert.DeserializeObject<DirectionsResult>(response);
Expand All @@ -63,7 +64,6 @@ public async Task<DirectionsResult> Route(DirectionsRequest request, DirectionsR
Console.WriteLine("Error parsing DirectionsResult Object. Message: " + e.Message);
return null;
}

}
}
}
3 changes: 2 additions & 1 deletion ServerSideDemo/Pages/MapRoutes.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

<h1>Google Map</h1>

<GoogleMap @ref="@map1" Id="map1" Options="@mapOptions" Height="350px" OnAfterInit="@(async () => await OnAfterInitAsync())"></GoogleMap>
<GoogleMap @ref="@_map1" Id="map1" Options="@_mapOptions" Height="350px" OnAfterInit="@(async () => await OnAfterInitAsync())"></GoogleMap>
<button @onclick="@AddDirections">Add Direction</button>
<button @onclick="@RemoveRoute">Remove route</button>
<button @onclick="@RunDirectionsService">RunDirectionsService</button>
<p>
Duration: @_durationTotalString <br />
Distance: @_distanceTotalString <br />
Expand Down
172 changes: 96 additions & 76 deletions ServerSideDemo/Pages/MapRoutes.razor.cs
Original file line number Diff line number Diff line change
@@ -1,99 +1,119 @@
using System;
using GoogleMapsComponents;
using GoogleMapsComponents.Maps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GoogleMapsComponents;
using GoogleMapsComponents.Maps;

namespace ServerSideDemo.Pages
namespace ServerSideDemo.Pages;

public partial class MapRoutes : IAsyncDisposable
{
public partial class MapRoutes : IAsyncDisposable
private GoogleMap? _map1;
private MapOptions _mapOptions;
private DirectionsRenderer _dirRend;
private string _durationTotalString;
private string _distanceTotalString;
private DirectionsResult _directionsResult;

protected override void OnInitialized()
{
private GoogleMap? map1;
private MapOptions mapOptions;
private DirectionsRenderer dirRend;
private string _durationTotalString;
private string _distanceTotalString;
private DirectionsResult _directionsResult;

protected override void OnInitialized()
_mapOptions = new MapOptions()
{
mapOptions = new MapOptions()
Zoom = 13,
Center = new LatLngLiteral()
{
Zoom = 13,
Center = new LatLngLiteral()
{
Lat = 40.603629,
Lng = -75.472518
},
MapTypeId = MapTypeId.Roadmap
};
}
Lat = 40.603629,
Lng = -75.472518
},
MapTypeId = MapTypeId.Roadmap
};
}

private async Task RemoveRoute()
{
await _dirRend.SetMap(null);

private async Task RemoveRoute()
_durationTotalString = null;
_distanceTotalString = null;
}

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

_durationTotalString = null;
_distanceTotalString = null;
}
private async Task AddDirections()
{
_durationTotalString = null;
_distanceTotalString = null;
if (await _dirRend.GetMap() is null) await _dirRend.SetMap(_map1!.InteropObject);

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

private async Task OnAfterInitAsync()
//Direction Request
var dr = new DirectionsRequest();
dr.Origin = "Allentown, PA";
dr.Destination = "Bronx, NY";
dr.Waypoints = waypoints;
dr.TravelMode = TravelMode.Driving;
dr.DrivingOptions = new DrivingOptions()
{
//Create instance of DirectionRenderer
dirRend = await DirectionsRenderer.CreateAsync(map1.JsRuntime, new DirectionsRendererOptions()
{
Map = map1.InteropObject
});
}
DepartureTime = DateTime.Now.AddHours(1)
};

private async Task AddDirections()
//Calculate Route
_directionsResult = await _dirRend.Route(dr, new DirectionsRequestOptions()
{
_durationTotalString = null;
_distanceTotalString = null;
if (await dirRend.GetMap() is null) await dirRend.SetMap(map1!.InteropObject);

//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)
};
StripLegsStepsLatLngs = false,
StripOverviewPath = false,
StripOverviewPolyline = false,
StripLegsStepsPath = false,
StripLegsSteps = false
});

//Calculate Route
_directionsResult = await dirRend.Route(dr, new DirectionsRequestOptions()
{
StripLegsStepsLatLngs = false,
StripOverviewPath = false,
StripOverviewPolyline = false,
StripLegsStepsPath = false,
StripLegsSteps = false
});
var routes = _directionsResult.Routes.SelectMany(x => x.Legs).ToList();

var routes = _directionsResult.Routes.SelectMany(x => x.Legs).ToList();
foreach (var route in routes)
{
_durationTotalString += route.DurationInTraffic?.Text;
_distanceTotalString += route.Distance.Text;
}
}

foreach (var route in routes)
{
_durationTotalString += route.DurationInTraffic?.Text;
_distanceTotalString += route.Distance.Text;
}
public async ValueTask DisposeAsync()
{
if (_dirRend is not null)
{
await _dirRend.SetMap(null);
_dirRend.Dispose();
}
}

private async Task RunDirectionsService()
{
var service = await DirectionsService.CreateAsync(_map1!.JsRuntime);

public async ValueTask DisposeAsync()
DirectionsRequest request = new()
{
if (dirRend is not null)
{
await dirRend.SetMap(null);
dirRend.Dispose();
}
Origin = "Orlando, FL",
Destination = "Miami, FL"
};

var result = await service.Route(request);
if (result == null)
{
Console.WriteLine("No result");
}
else
{
Console.WriteLine(result.Routes?.FirstOrDefault()?.Summary ?? "No routes?");
}
}
}
}

0 comments on commit e0301cb

Please sign in to comment.