Click here to Skip to main content
15,885,366 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.5K   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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Imaging;
using DeepCast.Services;

namespace DeepCast
{
    public partial class MainForm : Form
    {
        private LocationManager _locationManager;
        private Thread _locationThread;
        private List<GeoService> _geoServices;
        private LocationTextService _formsService = new LocationTextService();
        private LocationImageService _mapService = new LocationImageService();

        public MainForm()
        {
            InitializeComponent();

            _formsService.LocationChanged += new LocationChangedDelegate(_formsService_LocationChanged);
            _mapService.MapChanged += new MapImageChangedDelegate(_mapService_MapChanged);

            _locationManager = new LocationManager();

            _locationThread = new Thread(new ThreadStart(LoadServices));
            _locationThread.Start();
        }

        private void _mapService_MapChanged(string mapUrl)
        {
            if (this.InvokeRequired)
            {
                MapImageChangedDelegate method = new MapImageChangedDelegate(_mapService_MapChanged);
                this.Invoke(method, new object[] { mapUrl });
            }
            else
            {
                _mapImage.Url = new Uri(mapUrl);
            }
        }

        private void _formsService_LocationChanged(DeepCast.Location.GeoLocation location)
        {
            if (this.InvokeRequired)
            {
                LocationChangedDelegate method = new LocationChangedDelegate(_formsService_LocationChanged);
                this.Invoke(method, new object[] {location});
            }
            else
            {
                _latitudeLabel.Text = location.Latitude.ToStringNumeric();
                _longitudeLabel.Text = location.Longitude.ToStringNumeric();
            }
        }
        /// <summary>
        /// Load the services to be notified of location changes
        /// </summary>
        private void LoadServices()
        {
            _geoServices = new List<GeoService>()
            {
                _formsService,
                _mapService,
                //new FireEagleService(),
                new TwitterService(),
                //new YOURSERVICEHERE()
            };

            _locationManager.Attach(_formsService);
            _locationManager.Attach(_mapService);

            //if (Settings.FireEagleEnabled)
            //{
            //    _locationManager.Attach(_geoServices.Find(s => s.Name == "FireEagle"));
            //}

            //if (Settings.TwitterEnabled)
            //{
                _locationManager.Attach(_geoServices.Find(s => s.Name == "Twitter"));
            //}
        }
        /// <summary>
        /// Toggles the running state of the location providers.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void _startStopMenu_Click(object sender, EventArgs e)
        {
            _startStopMenu.Text = _locationManager.Running ? "Start" : "Stop";

            if (!_locationManager.Running)
            {
                _locationManager.Start();
            }
            else 
            {
                _locationManager.Stop();
            }
        }
        /// <summary>
        /// Display Fire Eagle settings
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void _fireEagleMenu_Click(object sender, EventArgs e)
        {
            FireEagleForm form = new FireEagleForm();
            DialogResult result = form.ShowDialog();

            if (result == DialogResult.OK)
            {
                var service = _geoServices.Where(s => s.Name == "FireEagle").First();

                if (Settings.FireEagleEnabled)
                {
                    _locationManager.Attach(service);
                }
                else
                {
                    _locationManager.Detatch(service);
                }
            }
        }
        /// <summary>
        /// Display Twitter settings
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void _twitterMenu_Click(object sender, EventArgs e)
        {
            TwitterForm form = new TwitterForm();
            DialogResult result = form.ShowDialog();

            if (result == DialogResult.OK)
            {
                var service = _geoServices.Where(s => s.Name == "Twitter").First();

                if (Settings.TwitterEnabled)// Changed by JoP
                {
                    _locationManager.Attach(service);
                }
                else
                {
                    _locationManager.Detatch(service);
                }
            }
        }
        /// <summary>
        /// Redraw the map with a new zoom level
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void _drawButton_Click(object sender, EventArgs e)
        {
            _mapService.Zoom = _zoomSlider.Value;
            _mapService.Update();
        }
    }
}

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