PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Geocoding/Artem.GoogleGeocoding/GeoLocation.cs

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