Example #1
0
        public static Directions GetDirections(LatLon origen, LatLon destino, string mode, string avoid, params LatLon[] waypoints)
        {
            try
            {
                var baseUrl = string.Format(DirectionsUrl, origen, destino, mode);

                if (!string.IsNullOrEmpty(avoid))
                {
                    baseUrl += string.Concat("&avoid=", avoid);
                }

                if (waypoints != null && waypoints.Length > 0)
                {
                    baseUrl += "&waypoints=" + waypoints.Aggregate(string.Empty, (w, y) => string.Concat(w, "|", y));
                    var index = baseUrl.IndexOf('|');
                    baseUrl = baseUrl.Remove(index, 1);
                }

                var xml = new XmlDocument();
                xml.Load(baseUrl);

                var status = xml.GetXmlNode("status");
                if (status == null || status.InnerText != "OK")
                {
                    return(null);
                }
                var route = xml.GetXmlNode("route");
                if (route == null)
                {
                    return(null);
                }

                var ci = CultureInfo.InvariantCulture;

                var summary = route.GetInnerText("summary");

                var directions = new Directions
                {
                    Descripcion = summary
                };

                var legs = route.GetChilds("leg");

                foreach (var leg in legs)
                {
                    var steps = leg.GetChilds("step");

                    var directionsLeg = new DirectionsLeg();
                    directions.Legs.Add(directionsLeg);

                    foreach (var step in steps)
                    {
                        var stepstartlat     = step.GetChild("start_location").GetInnerText("lat");
                        var stepstartlon     = step.GetChild("start_location").GetInnerText("lng");
                        var stependlat       = step.GetChild("end_location").GetInnerText("lat");
                        var stependlon       = step.GetChild("end_location").GetInnerText("lng");
                        var stepduration     = step.GetChild("duration").GetInnerText("value");
                        var stepdistance     = step.GetChild("distance").GetInnerText("value");
                        var steppolyline     = step.GetChild("polyline").GetInnerText("points");
                        var stepinstructions = step.GetInnerText("html_instructions");

                        var directionsStep = new DirectionsStep
                        {
                            StartLocation =
                                new LatLon(Convert.ToDouble(stepstartlat, ci),
                                           Convert.ToDouble(stepstartlon, ci)),
                            EndLocation =
                                new LatLon(Convert.ToDouble(stependlat, ci),
                                           Convert.ToDouble(stependlon, ci)),
                            Duration     = TimeSpan.FromSeconds(Convert.ToInt32(stepduration, ci)),
                            Distance     = Convert.ToDouble(stepdistance, ci),
                            Points       = DecodePolylinePoints(steppolyline),
                            Instructions = stepinstructions
                        };

                        directionsLeg.Steps.Add(directionsStep);
                    }
                }

                return(directions);
            }
            catch (Exception ex)
            {
                STrace.Exception("GoogleDirections", ex);
                return(null);
            }
        }
Example #2
0
        private static List <LatLon> DecodePolylinePoints(string encodedPoints)
        {
            if (string.IsNullOrEmpty(encodedPoints))
            {
                return(null);
            }
            var poly          = new List <LatLon>();
            var polylinechars = encodedPoints.ToCharArray();
            var index         = 0;

            var currentLat = 0;
            int currentLng = 0;
            int next5bits;
            int sum;
            int shifter;

            try
            {
                while (index < polylinechars.Length)
                {
                    // calculate next latitude
                    sum     = 0;
                    shifter = 0;
                    do
                    {
                        next5bits = (int)polylinechars[index++] - 63;
                        sum      |= (next5bits & 31) << shifter;
                        shifter  += 5;
                    } while (next5bits >= 32 && index < polylinechars.Length);

                    if (index >= polylinechars.Length)
                    {
                        break;
                    }

                    currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);

                    //calculate next longitude
                    sum     = 0;
                    shifter = 0;
                    do
                    {
                        next5bits = (int)polylinechars[index++] - 63;
                        sum      |= (next5bits & 31) << shifter;
                        shifter  += 5;
                    } while (next5bits >= 32 && index < polylinechars.Length);

                    if (index >= polylinechars.Length && next5bits >= 32)
                    {
                        break;
                    }

                    currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
                    var p = new LatLon(Convert.ToDouble(currentLat) / 100000.0,
                                       Convert.ToDouble(currentLng) / 100000.0);
                    poly.Add(p);
                }
            }
            catch (Exception)
            {
                // logo it
            }
            return(poly);
        }