PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Geocoding/Artem.GoogleGeocoding/GeoResponse.cs

#
C# | 123 lines | 77 code | 20 blank | 26 comment | 8 complexity | c0fc57298c011cc9fab06b7254c191aa MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Artem.Google.Net {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public partial class GeoResponse : IEnumerable<GeoResult> {
  10. #region Properties ///////////////////////////////////////////////////////////////////////
  11. /// <summary>
  12. /// Gets or sets the results.
  13. /// </summary>
  14. /// <value>The results.</value>
  15. public IList<GeoResult> Results { get; protected internal set; }
  16. /// <summary>
  17. /// Gets or sets the status.
  18. /// </summary>
  19. /// <value>The status.</value>
  20. public GeoStatus Status { get; protected internal set; }
  21. #endregion
  22. #region Construct / Destruct //////////////////////////////////////////////////////////////
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="GeoResponse"/> class.
  25. /// </summary>
  26. internal GeoResponse(JsonGeoData json) {
  27. this.Status = json.status;
  28. List<GeoResult> results = new List<GeoResult>();
  29. if (this.Status == GeoStatus.OK) {
  30. foreach (var r in json.results) {
  31. List<GeoAddress> addressComponents = new List<GeoAddress>(r.address_components.Length);
  32. foreach (var ac in r.address_components) {
  33. addressComponents.Add(new GeoAddress {
  34. LongName = ac.long_name,
  35. ShortName = ac.short_name,
  36. Types = ac.types
  37. });
  38. }
  39. Geometry geometry = new Geometry() { LocationType = r.geometry.location_type };
  40. if (r.geometry.bounds != null) {
  41. geometry.Bounds = new GeoBounds {
  42. NorthEast = new GeoLocation {
  43. Latitude = r.geometry.bounds.northeast.lat,
  44. Longitude = r.geometry.bounds.northeast.lng
  45. },
  46. SouthWest = new GeoLocation {
  47. Latitude = r.geometry.bounds.southwest.lat,
  48. Longitude = r.geometry.bounds.southwest.lng
  49. }
  50. };
  51. }
  52. if (r.geometry.location != null) {
  53. geometry.Location = new GeoLocation {
  54. Latitude = r.geometry.location.lat,
  55. Longitude = r.geometry.location.lng
  56. };
  57. }
  58. if (r.geometry.viewport != null) {
  59. geometry.Viewport = new GeoBounds {
  60. NorthEast = new GeoLocation {
  61. Latitude = r.geometry.viewport.northeast.lat,
  62. Longitude = r.geometry.viewport.northeast.lng
  63. },
  64. SouthWest = new GeoLocation {
  65. Latitude = r.geometry.viewport.southwest.lat,
  66. Longitude = r.geometry.viewport.southwest.lng
  67. }
  68. };
  69. }
  70. results.Add(new GeoResult {
  71. AddressComponents = addressComponents,
  72. FormattedAddress = r.formatted_address,
  73. Geometry = geometry,
  74. PartialMatch = r.partial_match,
  75. Types = r.types
  76. });
  77. }
  78. }
  79. this.Results = results;
  80. }
  81. #endregion
  82. #region Methods ///////////////////////////////////////////////////////////////////////////
  83. /// <summary>
  84. /// Returns an enumerator that iterates through the collection.
  85. /// </summary>
  86. /// <returns>
  87. /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
  88. /// </returns>
  89. public IEnumerator<GeoResult> GetEnumerator() {
  90. return this.Results.GetEnumerator();
  91. }
  92. /// <summary>
  93. /// Returns an enumerator that iterates through a collection.
  94. /// </summary>
  95. /// <returns>
  96. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  97. /// </returns>
  98. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
  99. return this.Results.GetEnumerator();
  100. }
  101. #endregion
  102. }
  103. }