//----------------------------------------------------------------------- // // Copyright (c) 2016 Mapbox. All rights reserved. // //----------------------------------------------------------------------- namespace Mapbox.Directions { using System; using System.Collections.Generic; using Mapbox.Utils; using Platform; /// A directions request. public class DirectionResource : Resource { private string apiEndpoint = "directions/v5/"; // Required private RoutingProfile profile; // Optional private Vector2d[] coordinates; // Optional private bool? alternatives; // Optional private BearingFilter[] bearings; // Optional private bool? continueStraight; // Optional private Overview overview; // Optional private double[] radiuses; // Optional private bool? steps; /// Initializes a new instance of the class. /// /// Array of LatLng points along route, between 2 and 25 elements in length. /// /// /// A routing profile, for all profile options. /// public DirectionResource(Vector2d[] coordinates, RoutingProfile profile) { this.Coordinates = coordinates; this.RoutingProfile = profile; } /// Gets the API endpoint as a partial URL path. public override string ApiEndpoint { get { return this.apiEndpoint; } } /// /// Gets or sets the coordinates. Array of LatLng points along route, /// between 2 and 25 elements in length. /// public Vector2d[] Coordinates { get { return this.coordinates; } set { if (value.Length < 2 || value.Length > 25) { throw new Exception("Must be between 2 and 25 elements in coordinates array."); } this.coordinates = value; } } /// /// Gets or sets the routing profile, for all profile options. /// public RoutingProfile RoutingProfile { get { return this.profile; } set { this.profile = value; } } /// /// Gets or sets the alternative option. Controls whether direction request should /// return alternative routes. /// public bool? Alternatives { get { return this.alternatives; } set { this.alternatives = value; } } /// /// Gets or sets the bearing option. An array of bearing filters. Each filter is composed of /// a bearing as decimal degrees clockwise between 0 and 360, and a range of variation from /// the bearing as decimal degrees between 0 and 180. /// public BearingFilter[] Bearings { get { return this.bearings; } set { if (value != null && value.Length != this.coordinates.Length) { throw new Exception("There must be as many bearings as there are coordinates in the request."); } this.bearings = value; } } /// /// Gets or sets the continue_straight option. Controls whether to route will /// continue in same direction of travel or if route may continue in opposite /// direction of travel at intermediate waypoints. /// public bool? ContinueStraight { get { return this.continueStraight; } set { this.continueStraight = value; } } /// /// Gets or sets the overview option. See for all overview options. /// public Overview Overview { get { return this.overview; } set { this.overview = value; } } /// /// Gets or sets the radiuses option. Controls maximum distance in meters that /// each coordinate is allowed to move when snapped to a nearby road segment. /// public double[] Radiuses { get { return this.radiuses; } set { if (value != null) { if (value.Length != this.coordinates.Length) { throw new Exception("There must be as many radiuses as there are coordinates in the request."); } for (int i = 0; i < value.Length; i++) { if (value[i] <= 0) { throw new Exception("Radius must be greater than 0"); } } } this.radiuses = value; } } /// Gets or sets the steps option. Controls whether to return steps and turn-by-turn instructions. public bool? Steps { get { return this.steps; } set { this.steps = value; } } /// /// Gets the URL string. /// /// The URL string. public override string GetUrl() { Dictionary opts = new Dictionary(); if (this.Alternatives != null) { opts.Add("alternatives", this.Alternatives.ToString().ToLower()); } if (this.Bearings != null) { opts.Add("bearings", GetUrlQueryFromArray(this.Bearings, ";")); } if (this.ContinueStraight != null) { opts.Add("continue_straight", this.ContinueStraight.ToString().ToLower()); } if (this.Overview != null) { opts.Add("overview", this.Overview.ToString()); } if (this.Radiuses != null) { opts.Add("radiuses", GetUrlQueryFromArray(this.Radiuses)); } if (this.Steps != null) { opts.Add("steps", this.Steps.ToString().ToLower()); } return Constants.BaseAPI + this.ApiEndpoint + this.RoutingProfile + GetUrlQueryFromArray(this.Coordinates, ";") + ".json" + EncodeQueryString(opts); } } }