Exemplo n.º 1
0
        public static string[] GetDistanceKmHrs(double latitude, double longitude, Address destination)
        {
            string url = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins={0},{1}&destinations={2},{3}&mode=driving&language=en-US&sensor=false";
            url = string.Format(url, latitude.ToString(), longitude.ToString(), destination.Latitude, destination.Longitude);

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            WebResponse webResponse = httpRequest.GetResponse();
            Stream dataStream = webResponse.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            webResponse.Close();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseFromServer);
            XmlNodeList status = xmlDoc.GetElementsByTagName("status");

            if (status.Item(0).InnerText == "OK")
            {
                XmlNodeList distanceNode = xmlDoc.GetElementsByTagName("distance");
                XmlNodeList durationNode = xmlDoc.GetElementsByTagName("duration");

                List<string> result = new List<string>();
                result.Add(distanceNode[0].ChildNodes[1].InnerText);
                result.Add(durationNode[0].ChildNodes[1].InnerText);
                return result.ToArray();
            }
            return null;
        }
Exemplo n.º 2
0
        public static Address GetAddressCoordenates(string street, int number, string city, string postalCode, string province, string country)
        {
            Address address = new Address()
                                  {
                                      City = city,
                                      Country = country,
                                      Number = number,
                                      Street = street,
                                      ZipCode = postalCode
                                  };

            string destinationFormat = "{0} {1}, {2}, {3} Province, {4}";
            var destination = string.Format(destinationFormat, street, number.ToString(), postalCode != string.Empty ? postalCode : city, province, country);
            destination = Uri.EscapeDataString(destination);
            string url = @"http://maps.googleapis.com/maps/api/geocode/xml?address="+destination+"&sensor=false";
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

            WebResponse webResponse = httpRequest.GetResponse();
            Stream dataStream = webResponse.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            webResponse.Close();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseFromServer);
            XmlNodeList status = xmlDoc.GetElementsByTagName("status");

            if(status.Item(0).InnerText == "OK")
            {
                XmlNodeList locationNode = xmlDoc.GetElementsByTagName("location");

                address.Latitude = float.Parse(locationNode[0].ChildNodes[0].InnerText);
                address.Longitude = float.Parse(locationNode[0].ChildNodes[1].InnerText);
                return address;
            }
            return null;
        }