-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathNavigationService.cs
92 lines (81 loc) · 3.32 KB
/
NavigationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using Esri.ArcGISRuntime;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Http;
using Esri.ArcGISRuntime.Security;
using Esri.ArcGISRuntime.Tasks.Geocoding;
using Esri.ArcGISRuntime.Tasks.NetworkAnalysis;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace RoutingSample.Services
{
public struct SolveRouteResult
{
/// <summary>
/// The route result.
/// </summary>
public RouteResult Route;
/// <summary>
/// The route task.
/// </summary>
public RouteTask Task;
/// <summary>
/// The route parameters.
/// </summary>
public RouteParameters Parameters;
}
public class NavigationService
{
private const string LocatorService = "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
private const string RouteService = "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
private static readonly Uri s_locatorService = new Uri(LocatorService);
private static readonly Uri s_routeService = new Uri(RouteService);
/// <summary>
/// Returns the location of the specified address.
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static async Task<MapPoint> GeocodeAsync(string address)
{
var locator = await LocatorTask.CreateAsync(s_locatorService);
var result = await locator.GeocodeAsync(address).ConfigureAwait(false);
return result?.FirstOrDefault()?.RouteLocation;
}
/// <summary>
/// Returns a route between the specified stops.
/// </summary>
/// <returns></returns>
public static async Task<SolveRouteResult> SolveRouteAsync(MapPoint from, MapPoint to)
{
RouteTask routeTask = null;
RouteParameters routeParameters = null;
RouteResult routeResult = null;
try
{
// Create a new route using the onling routing service
routeTask = await RouteTask.CreateAsync(s_routeService);
// Configure the route and set the stops
routeParameters = await routeTask.CreateDefaultParametersAsync();
routeParameters.SetStops(new[] { new Stop(from), new Stop(to) });
routeParameters.ReturnStops = true;
routeParameters.ReturnDirections = true;
routeParameters.RouteShapeType = RouteShapeType.TrueShapeWithMeasures;
routeParameters.OutputSpatialReference = SpatialReferences.Wgs84;
routeParameters.DirectionsDistanceUnits = UnitSystem.Metric;
routeParameters.StartTime = DateTime.UtcNow;
// Solve the route
routeResult = await routeTask.SolveRouteAsync(routeParameters);
}
catch (ArcGISWebException ex)
{
// Any error other than failure to locate is rethrown
if (ex.Details.FirstOrDefault()?.Contains("Unlocated") != true)
{
throw ex;
}
}
return new SolveRouteResult { Route = routeResult, Task = routeTask, Parameters = routeParameters };
}
}
}