PageRenderTime 63ms CodeModel.GetById 32ms app.highlight 6ms RepoModel.GetById 22ms app.codeStats 0ms

/Geocoding/Artem.GoogleGeocoding/GeoLocation.cs

#
C# | 154 lines | 66 code | 27 blank | 61 comment | 8 complexity | 959ab1df0f0e31f525ee0e4ee7371ba0 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Globalization;
  6
  7namespace Artem.Google.Net {
  8
  9    [Serializable]
 10    public class GeoLocation : IEquatable<GeoLocation> {
 11
 12        #region Static Methods ////////////////////////////////////////////////////////////////////
 13
 14        /// <summary>
 15        /// Implements the operator ==.
 16        /// </summary>
 17        /// <param name="x">The x.</param>
 18        /// <param name="y">The y.</param>
 19        /// <returns>The result of the operator.</returns>
 20        public static bool operator ==(GeoLocation x, GeoLocation y) {
 21            return object.ReferenceEquals(x, null)
 22                ? object.ReferenceEquals(y, null) : x.Equals(y);
 23        }
 24
 25        /// <summary>
 26        /// Implements the operator !=.
 27        /// </summary>
 28        /// <param name="x">The x.</param>
 29        /// <param name="y">The y.</param>
 30        /// <returns>The result of the operator.</returns>
 31        public static bool operator !=(GeoLocation x, GeoLocation y) {
 32            return !(x == y);
 33        }
 34
 35        /// <summary>
 36        /// Parses the specified point.
 37        /// </summary>
 38        /// <param name="point">The point.</param>
 39        /// <returns></returns>
 40        public static GeoLocation Parse(string point) {
 41
 42            if (point == null)
 43                throw new ArgumentNullException("point");
 44
 45            if (!string.IsNullOrEmpty(point)) {
 46
 47                point = point.Trim('(', ')');
 48                string[] pair = point.Split(',');
 49                if (pair.Length >= 2) {
 50                    var format = CultureInfo.GetCultureInfo("en").NumberFormat;
 51                    double lat = Convert.ToDouble(pair[0], format);
 52                    double lng = Convert.ToDouble(pair[1], format);
 53
 54                    return new GeoLocation(lat, lng);
 55                }
 56
 57            }
 58            throw new ArgumentException("Invalid GeoLocation string format.");
 59
 60        }
 61        #endregion
 62
 63        #region Properties  ///////////////////////////////////////////////////////////////////////
 64
 65        /// <summary>
 66        /// Gets or sets the latitude.
 67        /// </summary>
 68        /// <value>The latitude.</value>
 69        public double Latitude { get; set; }
 70
 71        /// <summary>
 72        /// Gets or sets the longitude.
 73        /// </summary>
 74        /// <value>The longitude.</value>
 75        public double Longitude { get; set; }
 76
 77        #endregion
 78
 79        #region Construct /////////////////////////////////////////////////////////////////////////
 80
 81        /// <summary>
 82        /// Initializes a new instance of the <see cref="GeoLocation"/> class.
 83        /// </summary>
 84        /// <param name="latitude">The latitude.</param>
 85        /// <param name="longitude">The longitude.</param>
 86        public GeoLocation(double latitude, double longitude) {
 87            this.Latitude = latitude;
 88            this.Longitude = longitude;
 89        }
 90
 91        /// <summary>
 92        /// Initializes a new instance of the <see cref="GeoLocation"/> class.
 93        /// </summary>
 94        /// <param name="source">The source.</param>
 95        public GeoLocation(GeoLocation source)
 96            : this(source.Latitude, source.Longitude) { }
 97
 98        /// <summary>
 99        /// Initializes a new instance of the <see cref="GeoLocation"/> class.
100        /// </summary>
101        public GeoLocation()
102            : this(0, 0) { }
103
104        #endregion
105
106        #region Methods ///////////////////////////////////////////////////////////////////////////
107
108        /// <summary>
109        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
110        /// </summary>
111        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
112        /// <returns>
113        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
114        /// </returns>
115        public override bool Equals(object obj) {
116            return (obj is GeoLocation) ? this.Equals(obj as GeoLocation) : false;
117        }
118
119        /// <summary>
120        /// Equalses the specified other.
121        /// </summary>
122        /// <param name="other">The other.</param>
123        /// <returns></returns>
124        public bool Equals(GeoLocation other) {
125            return !object.ReferenceEquals(other, null)
126                ? ((this.Latitude == other.Latitude) && (this.Longitude == other.Longitude))
127                : false;
128        }
129
130        /// <summary>
131        /// Returns a hash code for this instance.
132        /// </summary>
133        /// <returns>
134        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
135        /// </returns>
136        public override int GetHashCode() {
137            return this.Latitude.GetHashCode() ^ this.Longitude.GetHashCode();
138        }
139
140        /// <summary>
141        /// Returns a <see cref="System.String"/> that represents this instance.
142        /// </summary>
143        /// <returns>
144        /// A <see cref="System.String"/> that represents this instance.
145        /// </returns>
146        public override string ToString() {
147
148            var format = CultureInfo.GetCultureInfo("en").NumberFormat;
149            return string.Format("{0},{1}",
150                this.Latitude.ToString(format), this.Longitude.ToString(format));
151        }
152        #endregion
153    }
154}