PageRenderTime 163ms CodeModel.GetById 81ms app.highlight 6ms RepoModel.GetById 74ms app.codeStats 0ms

/Geocoding/Artem.GoogleGeocoding/GeoRequest.cs

#
C# | 187 lines | 79 code | 31 blank | 77 comment | 5 complexity | 2efe8ac0d4907e3025349e4cca8ae3c2 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using System.Linq;
  5using System.Net;
  6using System.Text;
  7using System.Web;
  8using System.Web.Script.Serialization;
  9
 10namespace Artem.Google.Net {
 11
 12    /// <summary>
 13    /// 
 14    /// </summary>
 15    public class GeoRequest {
 16
 17        #region Static Fields /////////////////////////////////////////////////////////////////////
 18
 19        static readonly string RequestUrl = "http://maps.google.com/maps/api/geocode/json?";
 20
 21        #endregion
 22
 23        #region Static Methods ////////////////////////////////////////////////////////////////////
 24
 25        /// <summary>
 26        /// Creates the specified address.
 27        /// </summary>
 28        /// <param name="address">The address.</param>
 29        /// <returns></returns>
 30        public static GeoRequest Create(string address) {
 31            return new GeoRequest(address);
 32        }
 33
 34        /// <summary>
 35        /// Creates the reverse.
 36        /// </summary>
 37        /// <param name="latitude">The latitude.</param>
 38        /// <param name="longitude">The longitude.</param>
 39        /// <returns></returns>
 40        public static GeoRequest CreateReverse(double latitude, double longitude) {
 41            return new GeoRequest(latitude, longitude);
 42        }
 43
 44        /// <summary>
 45        /// Creates the reverse.
 46        /// </summary>
 47        /// <param name="location">The location.</param>
 48        /// <returns></returns>
 49        public static GeoRequest CreateReverse(GeoLocation location) {
 50            return new GeoRequest(location);
 51        }
 52        #endregion
 53
 54        #region Properties  ///////////////////////////////////////////////////////////////////////
 55
 56        /// <summary>
 57        /// The address that you want to geocode.
 58        /// </summary>
 59        /// <value>The address.</value>
 60        public string Address { get; set; }
 61
 62        /// <summary>
 63        /// Gets or sets a value indicates whether or not the geocoding request comes from a device with a location sensor.
 64        /// </summary>
 65        /// <value><c>true</c> if this instance is sensor; otherwise, <c>false</c>.</value>
 66        public bool IsSensor { get; set; }
 67
 68        /// <summary>
 69        /// The language in which to return results. See the supported list of domain languages. 
 70        /// Note that we often update supported languages so this list may not be exhaustive. 
 71        /// If language is not supplied, the geocoder will attempt to use the native language of the domain 
 72        /// from which the request is sent wherever possible. 
 73        /// </summary>
 74        /// <value>The language.</value>
 75        public string Language { get; set; }
 76
 77        /// <summary>
 78        /// The latitude/longitude value for which you wish to obtain the closest, human-readable address.
 79        /// </summary>
 80        /// <value>The location.</value>
 81        public GeoLocation Location { get; set; }
 82
 83        /// <summary>
 84        /// The region code, specified as a ccTLD ("top-level domain") two-character value.
 85        /// </summary>
 86        /// <value>The region.</value>
 87        public string Region { get; set; }
 88
 89        #endregion
 90
 91        #region Construct /////////////////////////////////////////////////////////////////////////
 92
 93        /// <summary>
 94        /// Initializes a new instance of the <see cref="GeoRequest"/> class.
 95        /// </summary>
 96        /// <param name="address">The address.</param>
 97        public GeoRequest(string address) {
 98            this.Address = address;
 99        }
100
101        /// <summary>
102        /// Initializes a new instance of the <see cref="GeoRequest"/> class.
103        /// </summary>
104        /// <param name="location">The location.</param>
105        public GeoRequest(GeoLocation location) {
106            this.Location = location;
107        }
108
109        /// <summary>
110        /// Initializes a new instance of the <see cref="GeoRequest"/> class.
111        /// </summary>
112        /// <param name="latitude">The latitude.</param>
113        /// <param name="longitude">The longitude.</param>
114        public GeoRequest(double latitude, double longitude) {
115            this.Location = new GeoLocation(latitude, longitude);
116        }
117
118        /// <summary>
119        /// Initializes a new instance of the <see cref="GeoRequest"/> class.
120        /// </summary>
121        public GeoRequest() { }
122
123        #endregion
124
125        #region Methods ///////////////////////////////////////////////////////////////////////////
126
127        /// <summary>
128        /// Begins the get response.
129        /// </summary>
130        /// <param name="callback">The callback.</param>
131        /// <param name="state">The state.</param>
132        /// <returns></returns>
133        public IAsyncResult BeginGetResponse(AsyncCallback callback, object state) {
134            throw new NotImplementedException();
135        }
136
137        /// <summary>
138        /// Ends the get response.
139        /// </summary>
140        /// <param name="result">The result.</param>
141        /// <returns></returns>
142        public GeoResponse EndGetResponse(IAsyncResult result) {
143            throw new NotImplementedException();
144        }
145
146        /// <summary>
147        /// Gets the raw object.
148        /// </summary>
149        /// <returns></returns>
150        protected JsonGeoData GetJsonObject() {
151
152            StringBuilder url = new StringBuilder( GeoRequest.RequestUrl);
153            
154            if (this.Location != null) {
155                url.AppendFormat("latlng={0}", this.Location.ToString());
156            }
157            else {
158                url.AppendFormat("address={0}", HttpUtility.UrlEncode(this.Address));
159            }
160            url.AppendFormat("&sensor={0}", this.IsSensor.ToString().ToLower());
161            if (!string.IsNullOrEmpty(this.Language))
162                url.AppendFormat("&language={0}", HttpUtility.UrlEncode(this.Language));
163            if(!string.IsNullOrEmpty(this.Region))
164                url.AppendFormat("&region={0}", HttpUtility.UrlEncode(this.Region));
165
166            WebRequest request = WebRequest.Create(url.ToString());
167            string responseData;
168            using (WebResponse response = request.GetResponse()) {
169                using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
170                    responseData = reader.ReadToEnd();
171                }
172            }
173
174            JavaScriptSerializer serializer = new JavaScriptSerializer();
175            return serializer.Deserialize<JsonGeoData>(responseData);
176        }
177
178        /// <summary>
179        /// Gets the response.
180        /// </summary>
181        /// <returns></returns>
182        public GeoResponse GetResponse() {
183            return new GeoResponse(this.GetJsonObject());
184        }
185        #endregion
186    }
187}