Click here to Skip to main content
15,897,371 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.Collections.Specialized;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using MapPoint.SimpleClient.MapPoint.Service;


using J2i.Net.WiFiPositioning;
using J2i.Net.LocationSettings;


namespace MapPoint.SimpleClient
{
    public partial class Form1 : Form
    {
        
        Wps _wps = null;
        delegate void DisplayResultsDelegate(FindResult[] resultList);

        RegistrySettings _registrySettings = new RegistrySettings();

        DisplayResultsDelegate _displayResults;
        StringDictionary _entityDictionary = new StringDictionary();
        string[,] _entityData = new string[,] { 
        { "Manufacturing", "SICDivD" }, 
        {"Wholesale Trade", "SICDivF"}, 
        {"Retail Trade", "SICDivG"} ,
        {"Services","SICDivI"},
        {"Fast Food","FoodType27"},
        {"Indian Food","FoodType8"},
        {"Sea Food","FoodType13"}
        };

        public Form1()
        {
            InitializeComponent();
            _displayResults = new DisplayResultsDelegate(this.DisplayResults);

            for (int i = 0; i < _entityData.GetUpperBound(0); ++i)
            {
                _entityDictionary.Add(_entityData[i, 0], _entityData[i,1]);
                dupQuery.Items.Add(_entityData[i, 0]);
            }
            dupQuery.Text = _entityData[0, 0];
            ApplySettings();
        }

        void ApplySettings()
        {
            if (_wps != null)
                _wps.Dispose();
            _wps = new Wps(_registrySettings.SkyhookUser, _registrySettings.SkyhookRealm);
        }

        void RequestSettingsFromUser()
        {
            J2i.Net.LocationSettings.AccountForm af = new AccountForm();
            if (DialogResult.OK == af.ShowDialog())
            {
                _registrySettings.LoadApplicationKeyValues();
                ApplySettings();
            }
        }

        void LoadSettings()
        {
            _registrySettings.LoadApplicationKeyValues();
            if((_registrySettings.MapPointAppID.Length==0)||(_registrySettings.MapPointPassword.Length==0))
                ApplySettings();
        }

        private void miSearch_Click(object sender, EventArgs e)
        {
            MapPoint.Service.FindServiceSoap findService = new global::MapPoint.SimpleClient.MapPoint.Service.FindServiceSoap();

            //To prevent to annoying URI Exception in the debug output. Optional.
            findService.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy(); 

            //Specify our credentials for authorization
            findService.Credentials = new NetworkCredential(_registrySettings.MapPointAppID, _registrySettings.MapPointPassword);
            findService.PreAuthenticate = true;

            FindFilter findFilter = new FindFilter();

            //http://msdn.microsoft.com/en-us/library/cc534903.aspx
            findFilter.EntityTypeName = _entityDictionary[dupQuery.Text]; 
            
            //only display the display name for each search result
            findFilter.PropertyNames = new string[]{"DisplayName"};


            MapPoint.Service.LatLong latLong = new MapPoint.Service.LatLong();
            latLong.Latitude = Double.Parse(txtLatitude.Text);
            latLong.Longitude = Double.Parse(txtLongitude.Text);


            MapPoint.Service.FindNearbySpecification findSpecification = new MapPoint.Service.FindNearbySpecification();
            findSpecification.DataSourceName = _registrySettings.MapPointDataSource;
            //If the user has not selected a datasource then we will
            //use the NavTech North American datasource
            if (findSpecification.DataSourceName.Length == 0)
                findSpecification.DataSourceName = "NavTech.NA";
            findSpecification.LatLong = latLong;
            findSpecification.Distance = 100;
            findSpecification.Filter = findFilter;

            FindResults resultList = null;
            try
            {
                resultList = findService.FindNearby(findSpecification);
                if (resultList.Results != null)
                {
                    DisplayResults(resultList.Results);
                }
            }
            catch(Exception exc)
            {
                System.Diagnostics.Debug.WriteLine(exc.ToString());
            }
            
        }


        void DisplayResults(FindResult[] resultList)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(_displayResults, new object[] { resultList });
            }
            else
            {
                lstResults.Items.Clear();

                if ((resultList == null) || (resultList.Length == 0))
                {
                    lstResults.Items.Add("No Results");
                }
                else
                {
                    for (int i = 0; i < resultList.Length; ++i)
                    {
                        lstResults.Items.Add(resultList[i].FoundLocation.Entity.DisplayName);
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadSettings();
            UpdatePosition();
        }


        void UpdatePosition()
        {
            WiFiLocation location = null;
            if (ResultCode.OK == _wps.GetWiFiLocation(StreetAddressLookupType.NoStreet, out location))
            {
                this.txtLatitude.Text = location.Latitude.ToString();
                this.txtLongitude.Text = location.Longitude.ToString();
            }
            else
            {
                MessageBox.Show("Could not acquire location");
            }

        }
        private void miUpdatePosition_Click(object sender, EventArgs e)
        {
            UpdatePosition();
        }

        private void miSettings_Click(object sender, EventArgs e)
        {
            RequestSettingsFromUser();
        }
    }
}

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