Click here to Skip to main content
15,895,084 members
Articles / Mobile Apps

A GPS Keep-alive Utility and Tester for Windows Mobile

Rate me:
Please Sign up or sign in to vote.
4.82/5 (14 votes)
18 Jul 2008CPOL6 min read 111.3K   1.6K   67  
Keeps the GPS active in Windows Mobile, allowing for instant, accurate location determination. Also a tutorial on how to access GPS data with almost no code.
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
#region Using directives

using System;

#endregion

namespace Microsoft.WindowsMobile.Samples.Location
{
    /// <summary>
    /// class that represents a gps coordinate in degrees, minutes, and seconds.  
    /// </summary>
    public class DegreesMinutesSeconds
    {

        bool isPositive;
        /// <summary>
        /// Returns true if the degrees, minutes and seconds refer to a positive value,
        /// false otherwise.
        /// </summary>
        public bool IsPositive
        {
            get { return isPositive; }
        }

        uint degrees;
        /// <summary>
        /// The degrees unit of the coordinate
        /// </summary>
        public uint Degrees
        {
            get { return degrees; }
        }

        uint minutes;
        /// <summary>
        /// The minutes unit of the coordinate
        /// </summary>
        public uint Minutes
        {
            get { return minutes; }
        }

        double seconds;
        /// <summary>
        /// The seconds unit of the coordinate
        /// </summary>
        public double Seconds
        {
            get { return seconds; }
        }

        /// <summary>
        /// Constructs a new instance of DegreesMinutesSeconds converting 
        /// from decimal degrees
        /// </summary>
        /// <param name="decimalDegrees">Initial value as decimal degrees</param>
        public DegreesMinutesSeconds(double decimalDegrees)
        {
            isPositive = (decimalDegrees > 0);
            
            degrees = (uint) Math.Abs(decimalDegrees);
            
            double doubleMinutes = (Math.Abs(decimalDegrees) - Math.Abs((double)degrees)) * 60.0;
            minutes = (uint) doubleMinutes;

            seconds = (doubleMinutes - (double)minutes) * 60.0;
        }

        /// <summary>
        /// Constructs a new instance of DegreesMinutesSeconds
        /// </summary>
        /// <param name="isPositive">True if the coordinates are positive coordinate, false if they
        /// are negative coordinates.</param>
        /// <param name="degrees">Degrees unit of the coordinate</param>
        /// <param name="minutes">Minutes unit of the coordinate</param>
        /// <param name="seconds">Seconds unit of the coordinate. This should be a positive value.</param>
        public DegreesMinutesSeconds(bool isPositive, uint degrees, uint minutes, double seconds)
        {
            this.isPositive = isPositive;
            this.degrees = degrees;
            this.minutes = minutes;
            this.seconds = seconds;
        }

        /// <summary>
        /// Converts the decimal, minutes, seconds coordinate to 
        /// decimal degrees
        /// </summary>
        /// <returns></returns>
        public double ToDecimalDegrees()
        {
            double val = (double)degrees + ((double)minutes / 60.0) + ((double)seconds / 3600.0);
            val = isPositive ? val : val * -1;
            return val;
        }

        /// <summary>
        /// Converts the instance to a string in format: D M' S"
        /// </summary>
        /// <returns>string representation of degrees, minutes, seconds</returns>
        public override string ToString()
        {
            return degrees + "d " + minutes + "' " + seconds + "\"";
        }
    }
}

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
Team Leader World Golf Tour
Canada Canada
Sam Rahimi currently leads the web team at World Golf Tour (www.wgt.com) and has an extensive background in ASP.NET, specializing in social networking and casual gaming.

Previously, he has worked for Roblox Corporation on their unique children's MMO as well as spending two years as team lead at Supernova.com, helping bring their social networking site into the modern era and doubling traffic along the way.

Sam started as a classic ASP engineer as a summer job 6 years ago to make some money to pay for tuition - to finish a degree in political science - and needless to say, never looked back. His experience has lead him to gain additional experience in the mobile space - J2ME, Android app developmentm, and SMS protocols.

And sure, the other engineers in SF may have an IPhone - Rahimi sticks with the EVO all the way!

Comments and Discussions