コード例 #1
0
        public static string GetStatus(string address, string apiKey)
        {
            string response;

            try
            {
                response = HttpWebService.MakeRequest(
                    string.Format(
                        "{0}Locations/{1}?o=xml&key={2}", ID.MapsConsts.API_PREFIX, address, apiKey));
            }
            catch
            {
                throw new Exception(TX.Error.MAPS_CONNECTION_FAILED);
            }

            XmlDocument responseXml = new XmlDocument();

            responseXml.LoadXml(response);
            XmlNamespaceManager nameSpace = new XmlNamespaceManager(responseXml.NameTable);

            SharedFunctions.GenerateXmlNameSpace(ref nameSpace);
            XmlElement result = (XmlElement)responseXml.SelectSingleNode(
                string.Format("//{0}StatusDescription", ID.MapsConsts.XML_SCHEMA_TAG), nameSpace);
            string status = result.InnerText;

            return(status);
        }
        /// <summary>
        /// Gets a route from the Google Maps Directions web service.
        /// </summary>
        /// <param name="optimize">If set to <c>true</c> optimize the route by re-ordering the locations to minimize the
        /// time to complete the route.</param>
        public static Route GetRoute(string optimize, string apiKey, params GLocation[] locations)
        {
            if (locations.Length < 2)
            {
                throw new ArgumentException(TX.Error.MAPS_MISSING_REQUIRED_PARAMETERS);
            }

            /*Points inside a route are identified as: wp.n and vwp.n where wp demarks start and ed location and vwp demarks
             *  intermidle points inside route
             */
            string reqStr = null;

            for (int i = 0; i < locations.Length; i++)
            {
                reqStr += "wp." + i.ToString() + "=";
                reqStr += locations[i].ToString() + "&";
            }

            if (!string.IsNullOrEmpty(optimize))
            {
                reqStr += "optimize=" + optimize + "&"; // Options for optimize route are: distance, time [default], timeWithTraffic, timeAvoidClosure
            }
            reqStr = RemoveSpecialCharacters(reqStr);

            string response = null;
            int    attempts = 0;
            string status   = null;

            try
            {
                while (attempts < 3)
                {
                    response = HttpWebService.MakeRequest(
                        TX.MapsWebService.URL_PREFIX + reqStr + "key=" + apiKey);
                    status = GetStatus(response);

                    if ((!string.IsNullOrEmpty(status)) && (!string.Equals(status, ID.MapsStatusCodes.TOO_MANY_REQUESTS)))
                    {
                        break;
                    }

                    attempts++;
                    status = null;
                    Thread.Sleep(2000);
                }
            }catch
            {
                return(null);
            }

            return(ParseResponse(response));
        }
コード例 #3
0
        /// <summary>
        /// Geocodes the specified address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns>An array of possible locations.</returns>
        public static GLocation[] Geocode(string rAddress, string apiKey)
        {
            string      response;
            XmlDocument responseXml = new XmlDocument()
            {
                XmlResolver = null
            };

            try
            {
                string address = Regex.Replace(rAddress, "[^a-zA-Z0-9_. ]+", "", RegexOptions.Compiled);

                response = HttpWebService.MakeRequest(
                    string.Format(
                        "{0}Locations/{1}?o=xml&key={2}",
                        ID.MapsConsts.API_PREFIX,
                        address,
                        apiKey));

                responseXml.LoadXml(response);
            }
            catch
            {
                throw new Exception(TX.Error.MAPS_CONNECTION_FAILED);
            }

            //The XML document comes with a schema. So, was needed load the schema used by the site
            XmlNamespaceManager nameSpace = new XmlNamespaceManager(responseXml.NameTable);

            SharedFunctions.GenerateXmlNameSpace(ref nameSpace);

            XmlNodeList results = responseXml.SelectNodes(
                string.Format("//{0}Location",
                              ID.MapsConsts.XML_SCHEMA_TAG),
                nameSpace);

            List <GLocation> locations = new List <GLocation>();

            foreach (XmlElement result in results)
            {
                string formattedAddress = result.SelectSingleNode(
                    string.Format(".//{0}FormattedAddress", ID.MapsConsts.XML_SCHEMA_TAG), nameSpace).InnerText;
                XmlElement locationElement = (XmlElement)result.SelectSingleNode(
                    string.Format(".//{0}Point", ID.MapsConsts.XML_SCHEMA_TAG), nameSpace);

                LatLng    latLng   = new LatLng(locationElement, nameSpace);
                GLocation location = new GLocation(latLng, formattedAddress);
                locations.Add(location);
            }

            return(locations.ToArray());
        }
コード例 #4
0
        /// <summary>
        /// Reverses geocode the specified location.
        /// </summary>
        /// <param name="location">The location.</param>
        /// <returns>Returns the address of the location.</returns>
        public static string ReverseGeocode(LatLng location, string apiKey)
        {
            string response;

            try
            {
                response = HttpWebService.MakeRequest(
                    string.Format(
                        "{0}Locations/{1},{2}?o=xml&key={3}",
                        ID.MapsConsts.API_PREFIX,
                        location.Latitude,
                        location.Longitude,
                        apiKey));
            }
            catch
            {
                throw new Exception(TX.Error.MAPS_CONNECTION_FAILED);
            }

            XmlDocument responseXml = new XmlDocument()
            {
                XmlResolver = null
            };

            responseXml.LoadXml(response);

            //The XML document comes with a schema. So, was needed load the schema used by the site
            XmlNamespaceManager nameSpace = new XmlNamespaceManager(responseXml.NameTable);

            SharedFunctions.GenerateXmlNameSpace(ref nameSpace);
            string directionResult = string.Empty;

            string[] resultType = { "Address", "PopulatedPlace" };

            foreach (string type in resultType)
            {
                XmlNode result = responseXml.SelectSingleNode(
                    string.Format(
                        "//{0}Location[{0}EntityType = '{1}']//{0}FormattedAddress",
                        ID.MapsConsts.XML_SCHEMA_TAG,
                        type),
                    nameSpace);
                if (result != null)
                {
                    directionResult = result.InnerText;
                    break;
                }
            }

            return(directionResult);
        }