/// <summary>
 /// Gets the latitude and longitude that belongs to an address.
 /// </summary>
 /// <param name="address">The address.</param>
 /// <returns></returns>
 public MapPoint GetLatLongFromAddress(AddressData address) {
     return GetLatLongFromAddress(address.ToString());
 }
Exemple #2
0
        /// <summary>
        /// Gets the directions.
        /// </summary>
        /// <param name="originAddress">From address.</param>
        /// <param name="destinationAddress">To address.</param>
        /// <returns>The directions</returns>
        public Directions GetDirections(AddressData originAddress, AddressData destinationAddress)
        {
            var direction = new Directions();

            var xdoc = XDocument.Load(String.Format(ApiUrlDirections,
                                                    Uri.EscapeDataString(originAddress.ToString()),
                                                    Uri.EscapeDataString(destinationAddress.ToString())));

            var status = (from s in xdoc.Descendants("DirectionsResponse").Descendants("status")
                          select s).FirstOrDefault();

            if (status != null && status.Value == "OK")
            {
                direction.StatusCode = Directions.Status.Ok;
                var distance = (from d in xdoc.Descendants("DirectionsResponse").Descendants("route").Descendants("leg")
                                .Descendants("distance").Descendants("text")
                                select d).LastOrDefault();

                if (distance != null)
                {
                    direction.Distance = distance.Value;
                }

                var duration = (from d in xdoc.Descendants("DirectionsResponse").Descendants("route").Descendants("leg")
                                .Descendants("duration").Descendants("text")
                                select d).LastOrDefault();

                if (duration != null)
                {
                    direction.Duration = duration.Value;
                }

                var steps = from s in xdoc.Descendants("DirectionsResponse").Descendants("route").Descendants("leg").Descendants("step")
                            select s;

                foreach (var step in steps)
                {
                    var xElement = step.Element("html_instructions");
                    if (xElement == null)
                    {
                        continue;
                    }
                    var element = step.Descendants("distance").First().Element("text");
                    if (element == null)
                    {
                        continue;
                    }
                    var directionStep = new Step {
                        Instruction = xElement.Value,
                        Distance    = element.Value
                    };
                    direction.Steps.Add(directionStep);
                }
                return(direction);
            }
            if (status == null || status.Value == "OK")
            {
                throw new Exception("Unable to get Directions from Google");
            }
            direction.StatusCode = Directions.Status.Failed;
            return(direction);
        }
        /// <summary>
        /// Gets the directions.
        /// </summary>
        /// <param name="originAddress">From address.</param>
        /// <param name="destinationAddress">To address.</param>
        /// <returns>The directions</returns>
        public Directions GetDirections(AddressData originAddress, AddressData destinationAddress) {
            var direction = new Directions();

            var xdoc = XDocument.Load(String.Format(ApiUrlDirections,
                Uri.EscapeDataString(originAddress.ToString()),
                Uri.EscapeDataString(destinationAddress.ToString())));

            var status = (from s in xdoc.Descendants("DirectionsResponse").Descendants("status")
                          select s).FirstOrDefault();

            if (status != null && status.Value == "OK") {
                direction.StatusCode = Directions.Status.Ok;
                var distance = (from d in xdoc.Descendants("DirectionsResponse").Descendants("route").Descendants("leg")
                               .Descendants("distance").Descendants("text")
                                select d).LastOrDefault();

                if (distance != null) {
                    direction.Distance = distance.Value;
                }

                var duration = (from d in xdoc.Descendants("DirectionsResponse").Descendants("route").Descendants("leg")
                               .Descendants("duration").Descendants("text")
                                select d).LastOrDefault();

                if (duration != null) {
                    direction.Duration = duration.Value;
                }

                var steps = from s in xdoc.Descendants("DirectionsResponse").Descendants("route").Descendants("leg").Descendants("step")
                            select s;

                foreach (var step in steps) {
                    var xElement = step.Element("html_instructions");
                    if (xElement == null)
                        continue;
                    var element = step.Descendants("distance").First().Element("text");
                    if (element == null)
                        continue;
                    var directionStep = new Step {
                        Instruction = xElement.Value,
                        Distance = element.Value
                    };
                    direction.Steps.Add(directionStep);
                }
                return direction;
            }
            if (status == null || status.Value == "OK")
                throw new Exception("Unable to get Directions from Google");
            direction.StatusCode = Directions.Status.Failed;
            return direction;
        }
Exemple #4
0
 /// <summary>
 /// Gets the latitude and longitude that belongs to an address.
 /// </summary>
 /// <param name="address">The address.</param>
 /// <returns></returns>
 public MapPoint GetLatLongFromAddress(AddressData address)
 {
     return(GetLatLongFromAddress(address.ToString()));
 }