-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DirectionsService.Route is using old javascript function name #243
- Loading branch information
Showing
4 changed files
with
103 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?"); | ||
} | ||
} | ||
} | ||
} |