PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/Geocoding/Artem.GoogleGeocoding/GeoResponse.cs

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