Click here to Skip to main content
15,898,134 members
Articles / Mobile Apps

Using the Skyhook Wireless XPS Positioning Service in Managed Code

Rate me:
Please Sign up or sign in to vote.
4.95/5 (15 votes)
20 Jan 2009CPOL28 min read 89.2K   1.4K   57  
Wrapper and sample programs demonstrating the use of the Skyhook Wireless XPS SDK (hybrid position system using GPS, WiFi Positioning, and Celltower positioning)
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using J2i.Net.WiFiPositioning;

using J2i.Net.LocationSettings;

namespace SimpleClient
{
    public partial class SimpleClientForm : Form
    {

        delegate void DisplayLocationDelegate(WiFiLocation loc);
        delegate void VoidDelegate();

        Wps _wps = null;

        RegistrySettings _registry = new RegistrySettings();
        VoidDelegate _enableMenus;
        string _userName;
        string _realm;
        bool _stopPeriodic = false;
        ResultCode _lastResultCode;
        DisplayLocationDelegate _displayLocation;

        public SimpleClientForm()
        {
            ResultCode rc;
            InitializeComponent();
            _displayLocation = new DisplayLocationDelegate(this.UpdatePosition);
            _wps = new Wps(_registry.SkyhookUser, _registry.SkyhookRealm);
            
            _wps.WiFiLocationCallback = new WiFiLocationCallback(UpdatePosition);
            _enableMenus = new VoidDelegate(EnableMenuItems);
            rc = _wps.BeginTiling("\\temp\\", 500000, 4000000);
        }

        Continuation UpdatePosition(ResultCode rc, WiFiLocation loc)
        {
            UpdatePosition(loc);
            if (_stopPeriodic)
            {
                _stopPeriodic = false;
                EnableMenuItems();
                return Continuation.Stop;                
            }
            return Continuation.Continue;
        }

        void AppendListViewItem(string elementName, string elementValue)
        {
            ListViewItem lvi = new ListViewItem(new string[]{elementName,elementValue});
            lvPosition.Items.Add(lvi);
        }
        void UpdatePosition()
        {
            WiFiLocation loc;
            _lastResultCode = _wps.GetWiFiLocation(StreetAddressLookupType.NoStreet,out loc);
            this.UpdatePosition(loc);
        }

        void EnableMenuItems()
        {
            if (this.InvokeRequired)
                Invoke(_enableMenus);
            else
            {
                miFullAddress.Enabled = true;
                miNow.Enabled = true;
                miClose.Enabled = true;
            }
        }
        
        void UpdatePosition(WiFiLocation loc)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(_displayLocation, new object[] { loc });
            }
            else
            {
                statusBar.Text = _lastResultCode.ToString();
                lvPosition.Items.Clear();
                AppendListViewItem("TimeStamp", DateTime.Now.ToLongTimeString());
                if (loc == null)
                {
                    AppendListViewItem("Latitude", "Unknown");
                    AppendListViewItem("Longitude", "Unknown");
                }
                else
                {

                    AppendListViewItem("Latitude", loc.Latitude.ToString("0.000000"));
                    AppendListViewItem("Longitude", loc.Longitude.ToString("0.000000"));
                    AppendListViewItem("H.P.E.", loc.HorizontalPositioningError.ToString());
                    AppendListViewItem("Speed", String.Format("{0} km/h", loc.Speed));
                    AppendListViewItem("Bearing", String.Format("{0} degrees", loc.Bearing));
                    AppendListViewItem("Access Point Count", loc.AccessPointCount.ToString());
                    if (loc.StreetAddress != null)
                    {
                        AppendListViewItem("Street Number", loc.StreetAddress.StreetNumber);
                        AppendListViewItem("Address Line", loc.StreetAddress.AddressLine);
                        AppendListViewItem("County", loc.StreetAddress.County);
                        AppendListViewItem("State", loc.StreetAddress.State.Name);
                        AppendListViewItem("City", loc.StreetAddress.City);
                        AppendListViewItem("Postal Code", loc.StreetAddress.PostalCode);
                        AppendListViewItem("Country", loc.StreetAddress.Country.Name);
                        AppendListViewItem("Province", loc.StreetAddress.Province);
                        AppendListViewItem("Region", loc.StreetAddress.Region);
                    }
                }
            }
        }

        private void miUpdateLocation_Click(object sender, EventArgs e)
        {

        }

        private void miNow_Click(object sender, EventArgs e)
        {
            this.UpdatePosition();
        }

        

        void StartPeriodicLocation()
        {
            ResultCode rc = _wps.BeginPeriodicLocation(StreetAddressLookupType.NoStreet, 5000, 0);
        }

        void PeriodicLocationEnded(IAsyncResult ar)
        {

        }
        private void miPeriodically_Click(object sender, EventArgs e)
        {
            if (this.miPeriodically.Checked)
            {
                miPeriodically.Checked = false;
                _stopPeriodic = true;
            }
            else
            {
                this.miPeriodically.Checked = true;
                miNow.Enabled = false;
                miFullAddress.Enabled = false;
                miClose.Enabled = false;

                VoidDelegate v = new VoidDelegate(StartPeriodicLocation);

                ThreadStart ts = new ThreadStart(StartPeriodicLocation);
                Thread t = new Thread(ts);
                t.IsBackground = true;
                t.Start();
                
            }
        }

        private void SimpleClientForm_Load(object sender, EventArgs e)
        {
            //_wps = new Wps(_userName, _realm);
            
        }

        private void miClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void miFullAddress_Click(object sender, EventArgs e)
        {
            WiFiLocation loc;
            _lastResultCode = _wps.GetWiFiLocation(StreetAddressLookupType.FullStreet, out loc);
            this.UpdatePosition(loc);
        }

    }
}

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
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions