Click here to Skip to main content
15,881,380 members
Articles / Mobile Apps / Windows Mobile

Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant

Rate me:
Please Sign up or sign in to vote.
4.94/5 (126 votes)
10 Feb 2009CPOL10 min read 773.1K   11.1K   381  
A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace DeepCast.Location
{
    /// <summary>
    /// Represents latitude and longitude locations
    /// </summary>
    internal sealed class GeoLocation
    {
        /// <summary>
        /// Gets or sets the latitude.
        /// </summary>
        /// <value>The latitude.</value>
        public double Latitude { get; set; }
        /// <summary>
        /// Gets or sets the longitude.
        /// </summary>
        /// <value>The longitude.</value>
        public double Longitude { get; set; }

        /// <summary>
        /// Gets an empty GeoLocation instance.
        /// </summary>
        /// <value>Latitude and Longitude are set to double.NaN.</value>
        public static GeoLocation Empty
        {
            get { return new GeoLocation() { Latitude = double.NaN, Longitude = double.NaN }; }
        }
        /// <summary>
        /// Compares GeoLocation instances for equality
        /// </summary>
        /// <param name="toCompare">To compare.</param>
        /// <returns>True if equal; otherwise false</returns>
        public override bool Equals(object toCompare)
        {
            if (!(toCompare is GeoLocation))
            {
                return false;
            }

            return Equals((GeoLocation)toCompare);
        }
        /// <summary>
        /// Compares GeoLocation instances for equality
        /// </summary>
        /// <param name="toCompare">To compare.</param>
        /// <returns>True if equal; otherwise false</returns>
        public bool Equals(GeoLocation toCompare)
        {
            // Avoid null reference exceptions
            if (toCompare == null)
            {
                return false;
            }

            // Check comparison types for equality before object comparison
            if (GetType() != toCompare.GetType())
            {
                return false;
            }

            GeoLocation location = (GeoLocation)toCompare;

            // Check for equality of latitude
            if (!this.Latitude.Equals(location.Latitude))
            {
                return false;
            }

            // Check for equality of longitude
            return this.Longitude.Equals(toCompare.Longitude);
        }
        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return this.Latitude.GetHashCode() ^ this.Longitude.GetHashCode();
        }
        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="location1">The left hand location.</param>
        /// <param name="location2">The right hand location.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(GeoLocation location1, GeoLocation location2)
        {
            // Call ReferenceEquals to prevent recursion
            if (object.ReferenceEquals(location1, null))
            {
                return object.ReferenceEquals(location2, null);
            }

            if(object.ReferenceEquals(location2, null))
            {
                return false;
            }

            if (location1.GetType() != location2.GetType())
            {
                return false;
            }

            return location1.Equals(location2);
        }
        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="location1">The left hand location.</param>
        /// <param name="location2">The right hand location.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(GeoLocation location1, GeoLocation location2)
        {
            return !(location1 == location2);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!

http://www.pagelabs.com

Comments and Discussions