I have problem when I use the Online Rote tak to find the shortest driving distance.
The OnlineRouteTask class throws an exception 'System.ArgumentNullException: A value can not be null' in the function 'SolveAsync'. I have tried to set/change all the parameters in the call with no luck.
It used to work, so I am a little puzzled.
Here is my code:
async Task<double> FindDistance(double fromLat, double fromLon, double toLat, double toLon)
{
double distance = -1;
try
{
const string RouteUrl = "http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
var fromPoint = new MapPoint(fromLon, fromLat, SpatialReferences.Wgs84);
var fromSym = new SimpleMarkerSymbol { Style = SimpleMarkerStyle.X, Size = 10, Color = Colors.Green };
var fromMapGraphic = new Graphic { Geometry = fromPoint, Symbol = fromSym };
var toPoint = new MapPoint(toLon, toLat, SpatialReferences.Wgs84);
var toSym = new SimpleMarkerSymbol { Style = SimpleMarkerStyle.X, Size = 10, Color = Colors.GreenYellow };
var toMapGraphic = new Graphic { Geometry = toPoint, Symbol = toSym };
var routeTask = new OnlineRouteTask(new Uri(RouteUrl));
var routeParams = await routeTask.GetDefaultParametersAsync();
//routeParams.OutSpatialReference = ihmMapView.SpatialReference;
//routeParams.DirectionsLengthUnit = LinearUnits.Kilometers;
routeParams.ReturnDirections = false;
routeParams.OutputLines = OutputLine.None;
//routeParams.ReturnPointBarriers = true;
//routeParams.FindBestSequence = true;
var restrictions = new List<string>();
restrictions.Add("Avoid Ferries");
restrictions.Add("Driving an Emergency Vehicle");
routeParams.RestrictionAttributeNames = restrictions;
var stopGraphics = new List<Graphic>();
stopGraphics.Add(fromMapGraphic);
stopGraphics.Add(toMapGraphic);
routeParams.SetStops(stopGraphics);
var routeResult = await routeTask.SolveAsync(routeParams); <--- Exception is thrown here
foreach (var de in routeResult.Routes[0].TotalCosts)
{
if (de.Key == "Kilometers")
{
distance = de.Value;
break;
}
}
}
catch (Exception e)
{
log.Error(e);
}
return distance;
}
The Exception:
ERROR - System.ArgumentNullException: A value can not be null.
Parameternavn: source
ved System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.NetworkDescription.GetTimeAndLengthAttributes(NAParameters parameters, String& timeAttribute, String& lengthAttribute)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.NAParameters.GetParameters(NetworkDescription networkDescription, Boolean autoNormalize)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.RouteParameters.GetParameters(NetworkDescription networkDescription, Boolean autoNormalize)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.OnlineRouteTask.<SolveRequest>d__17.MoveNext()
--- Afslutningen på staksporingen fra den tidligere placering, hvor undtagelsen blev udløst ---
ved System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
ved System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ved Esri.ArcGISRuntime.Tasks.NetworkAnalyst.OnlineRouteTask.<Solve>d__12.MoveNext()
--- Afslutningen på staksporingen fra den tidligere placering, hvor undtagelsen blev udløst ---
ved System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
ved System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ved System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
ved InformiGis.IhmMapControl.<FindDistance>d__106.MoveNext() <--- This is my function
I believe you need an ArcGIS Online organizational account to use the world routing service. Are you using IdentityManager to set credentials for the service? I tested with your code and valid credentials and I see routes and distances generated as expected.
The 10.2.7 guide for accessing a secure service is here.