/// <summary>
    /// Converts the route obtained by OnlineMapsFindDirection, to array of list of the steps of the route.
    /// </summary>
    /// <param name="route">Route obtained by OnlineMapsFindDirection.</param>
    /// <returns>Array of list of OnlineMapsDirectionStep or null.</returns>
    public static List<OnlineMapsDirectionStep>[] TryParseWithAlternatives(string route)
    {
        try
        {
            OnlineMapsXML xml = OnlineMapsXML.Load(route);

            OnlineMapsXML direction = xml.Find("//DirectionsResponse");
            if (direction.isNull) return null;

            string status = direction.Get<string>("status");
            if (status != "OK") return null;

            OnlineMapsXMLList routes = direction.FindAll("route");
            List<OnlineMapsDirectionStep>[] result = new List<OnlineMapsDirectionStep>[routes.count];

            for(int i = 0; i < routes.count; i++)
            {
                OnlineMapsXMLList legNodes = routes[i].FindAll("leg");
                if (legNodes == null || legNodes.count == 0) continue;

                List<OnlineMapsDirectionStep> steps = new List<OnlineMapsDirectionStep>();

                foreach (OnlineMapsXML legNode in legNodes)
                {
                    OnlineMapsXMLList stepNodes = legNode.FindAll("step");
                    if (stepNodes.count == 0) continue;

                    foreach (OnlineMapsXML step in stepNodes)
                    {
                        OnlineMapsDirectionStep navigationStep = new OnlineMapsDirectionStep(step);
                        steps.Add(navigationStep);
                    }
                }

                result[i] = steps;
            }

            return result;
        }
        catch { }

        return null;
    }
    /// <summary>
    /// Converts the route obtained by OnlineMapsOpenRouteService, a list of the steps of the route.
    /// </summary>
    /// <param name="route">Route obtained by OnlineMapsOpenRouteService.</param>
    /// <returns>List of OnlineMapsDirectionStep or null.</returns>
    public static List<OnlineMapsDirectionStep> TryParseORS(string route)
    {
        try
        {
            OnlineMapsXML xml = OnlineMapsXML.Load(route);
            OnlineMapsXMLNamespaceManager nsmgr = xml.GetNamespaceManager();
            OnlineMapsXML errorNode = xml.Find("//xls:ErrorList/xls:Error", nsmgr);
            if (!errorNode.isNull) return null;

            OnlineMapsXMLList instructionNodes = xml.FindAll("//xls:RouteInstruction", nsmgr);
            List<OnlineMapsDirectionStep> steps = new List<OnlineMapsDirectionStep>();

            foreach (OnlineMapsXML node in instructionNodes)
            {
                OnlineMapsDirectionStep step = new OnlineMapsDirectionStep();
                step.points = new List<Vector2>();

                OnlineMapsXML geometry = node.Find("xls:RouteInstructionGeometry", nsmgr);
                OnlineMapsXML line = geometry[0];
                foreach (OnlineMapsXML pointNode in line)
                {
                    string coordsStr = pointNode.Value();
                    string[] coords = coordsStr.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
                    Vector2 coordsV = new Vector2(float.Parse(coords[0]), float.Parse(coords[1]));
                    step.points.Add(coordsV);
                }

                step.distance = (int)(node.Find("xls:Distance", nsmgr).A<float>("value") * 1000);
                step.instructions = node.Find("xls:Instruction", nsmgr).Value();

                steps.Add(step);
            }

            return steps;
        }
        catch (Exception exception)
        {
            Debug.Log("Exception: " + exception.Message + "\n" + exception.StackTrace);
        }
        return null;
    }
    /// <summary>
    /// Converts the route obtained by OnlineMapsFindDirection, a list of the steps of the route.
    /// </summary>
    /// <param name="route">Route obtained by OnlineMapsFindDirection.</param>
    /// <returns>List of OnlineMapsDirectionStep or null.</returns>
    public static List<OnlineMapsDirectionStep> TryParse(string route)
    {
        try
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(route);

            XmlNode direction = doc.SelectSingleNode("//DirectionsResponse");
            if (direction == null) return null;

            XmlNode status = direction.SelectSingleNode("status");
            if (status == null || status.InnerText != "OK") return null;

            XmlNode legNode = direction.SelectSingleNode("route/leg");
            if (legNode == null) return null;

            XmlNodeList stepNodes = legNode.SelectNodes("step");
            if (stepNodes == null) return null;

            List<OnlineMapsDirectionStep> steps = new List<OnlineMapsDirectionStep>();

            foreach (XmlNode step in stepNodes)
            {
                OnlineMapsDirectionStep navigationStep = new OnlineMapsDirectionStep(step);
                steps.Add(navigationStep);
            }

            return steps;
        }
        catch { }

        return null;
    }
    /// <summary>
    /// Converts the route obtained by OnlineMapsFindDirection, a list of the steps of the route.
    /// </summary>
    /// <param name="route">Route obtained by OnlineMapsFindDirection.</param>
    /// <returns>List of OnlineMapsDirectionStep or null.</returns>
    public static List<OnlineMapsDirectionStep> TryParse(string route)
    {
        try
        {
            OnlineMapsXML xml = OnlineMapsXML.Load(route);

            OnlineMapsXML direction = xml.Find("//DirectionsResponse");
            if (direction.isNull) return null;

            string status = direction.Find<string>("status");
            if (status != "OK") return null;

            OnlineMapsXMLList legNodes = direction.FindAll("route/leg");
            if (legNodes == null || legNodes.count == 0) return null;

            List<OnlineMapsDirectionStep> steps = new List<OnlineMapsDirectionStep>();

            foreach (OnlineMapsXML legNode in legNodes)
            {
                OnlineMapsXMLList stepNodes = legNode.FindAll("step");
                if (stepNodes.count == 0) continue;

                foreach (OnlineMapsXML step in stepNodes)
                {
                    OnlineMapsDirectionStep navigationStep = new OnlineMapsDirectionStep(step);
                    steps.Add(navigationStep);
                }
            }

            return steps;
        }
        catch { }

        return null;
    }
        public Step(OnlineMapsXML node)
        {
            List <Step> steps = new List <Step>();

            foreach (OnlineMapsXML n in node)
            {
                if (n.name == "travel_mode")
                {
                    travel_mode = n.Value();
                }
                else if (n.name == "start_location")
                {
                    start_location = OnlineMapsFindDirection.GetVector2FromNode(n);
                }
                else if (n.name == "end_location")
                {
                    end_location = OnlineMapsFindDirection.GetVector2FromNode(n);
                }
                else if (n.name == "polyline")
                {
                    polyline = OnlineMapsFindDirection.DecodePolylinePoints(n["points"].Value()).ToArray();
                }
                else if (n.name == "duration")
                {
                    duration = new TextValue <int>(n);
                }
                else if (n.name == "distance")
                {
                    distance = new TextValue <int>(n);
                }
                else if (n.name == "step")
                {
                    steps.Add(new Step(n));
                }
                else if (n.name == "html_instructions")
                {
                    html_instructions = n.Value();
                    if (string.IsNullOrEmpty(html_instructions))
                    {
                        return;
                    }
                    string_instructions = OnlineMapsDirectionStep.StrReplace(html_instructions,
                                                                             new[] { "&lt;", "&gt;", "&nbsp;", "&amp;", "&amp;nbsp;" },
                                                                             new[] { "<", ">", " ", "&", " " });
                    string_instructions = Regex.Replace(string_instructions, "<div.*?>", "\n");
                    string_instructions = Regex.Replace(string_instructions, "<.*?>", string.Empty);
                }
                else if (n.name == "maneuver")
                {
                    maneuver = n.Value();
                }
                else if (n.name == "transit_details")
                {
                    transit_details = new TransitDetails(n);
                }
                else
                {
                    Debug.Log("Step: " + n.name + "\n" + n.outerXml);
                }
            }

            this.steps = steps.ToArray();
        }