Click here to Skip to main content
15,886,258 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.6K   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;
using Microsoft.WindowsMobile.Samples.Location;

namespace DeepCast.Location
{
    /// <summary>
    /// Location Provider to get GPS data from a GPS enabled device or GPS peripheral
    /// </summary>
    internal class GpsLocationProvider : ILocationProvider
    {
        private Gps _gps = new Gps();
        private GpsPosition _currentPosition;

        ~GpsLocationProvider()
        {
            Stop();
        }

        #region ILocationProvider Members

        /// <summary>
        /// Starts polling for GPS changes.
        /// </summary>
        public void Start()
        {
            _gps.LocationChanged += new Microsoft.WindowsMobile.Samples.Location.LocationChangedEventHandler(_gps_LocationChanged);

            if (!_gps.Opened)
            {
                _gps.Open();
            }
        }
        /// <summary>
        /// Stops polling for GPS changes.
        /// </summary>
        public void Stop()
        {
            // Unsubscribe from event so the gps thread can obtain a lock for closing the connection
            _gps.LocationChanged -= new Microsoft.WindowsMobile.Samples.Location.LocationChangedEventHandler(_gps_LocationChanged);

            if (_gps.Opened)
            {
                _gps.Close();
            }
        }
        /// <summary>
        /// Gets the current location.
        /// </summary>
        /// <value>The current location.</value>
        public GeoLocation CurrentLocation
        {
            get
            {
                if (_currentPosition != null)
                {
                    return new GeoLocation() { Latitude = _currentPosition.Latitude, Longitude = _currentPosition.Longitude };
                }
                else
                {
                    return GeoLocation.Empty;
                }
            }
        }

        #endregion
        
        /// <summary>
        /// Handles the LocationChanged event of the _gps control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Microsoft.WindowsMobile.Samples.Location.LocationChangedEventArgs"/> instance containing the event data.</param>
        private void _gps_LocationChanged(object sender, Microsoft.WindowsMobile.Samples.Location.LocationChangedEventArgs args)
        {
            _currentPosition = args.Position;
        }
    }
}

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