PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Geocoding/Artem.GoogleGeocoding/GeoRequest.cs

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