gpsposition.cs

来自「清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码」· CS 代码 · 共 488 行 · 第 1/2 页

CS
488
字号
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#region Using directives

using System;
using System.Runtime.InteropServices;
using System.Collections;

#endregion

namespace Microsoft.WindowsMobile.Samples.Location
{    
    enum FixQuality : int
    {
        Unknown = 0,
        Gps,
        DGps
    }

    enum FixType : int
    {
        Unknown = 0,
        XyD,
        XyzD
    }

    enum FixSelection : int
    {
        Unknown = 0,
        Auto,
        Manual
    }

    public class Satellite
    {
        internal Satellite() { }

        internal Satellite(int id, int elevation, int azimuth, int signalStrength)
        {
            this.id = id;
            this.elevation = elevation;
            this.azimuth = azimuth;
            this.signalStrength = signalStrength;
        }

        int id;
        /// <summary>
        /// Id of the satellite
        /// </summary>
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        int elevation;
        /// <summary>
        /// Elevation of the satellite
        /// </summary>
        public int Elevation
        {
            get
            {
                return elevation;
            }
            set
            {
                elevation = value;
            }
        }

        int azimuth;
        /// <summary>
        /// Azimuth of the satellite
        /// </summary>
        public int Azimuth
        {
            get
            {
                return azimuth;
            }
            set
            {
                azimuth = value;
            }
        }

        int signalStrength;
        /// <summary>
        /// SignalStrenth of the satellite
        /// </summary>
        public int SignalStrength
        {
            get
            {
                return signalStrength;
            }
            set
            {
                signalStrength = value;
            }
        }

    }

    [StructLayout(LayoutKind.Sequential)]
    public class GpsPosition
    {
        private GPS_POSITION position;

        internal GpsPosition(GPS_POSITION position)
        {
            this.position = position;
        }
        
        /// <summary>
        /// UTC according to GPS clock.
        /// </summary>
        public DateTime Time
        {
            get
            {
                DateTime time = new DateTime(position.stUTCTime.year, position.stUTCTime.month, position.stUTCTime.day, position.stUTCTime.hour, position.stUTCTime.minute, position.stUTCTime.second, position.stUTCTime.millisecond);
                return time;
            }

        }
        /// <summary>
        /// True if the Time property is valid, false if invalid
        /// </summary>
        public bool TimeValid
        {
            get { return (position.dwValidFields & GPS_VALID.UTC_TIME) != 0; }
        }


        /// <summary>
        /// Satellites used in the solution
        /// </summary>
        /// <returns>Array of Satellites</returns>
        public Satellite[] GetSatellitesInSolution()
        {
            Satellite[] inViewSatellites = GetSatellitesInView();
            ArrayList list = new ArrayList();
            for (int index = 0; index < position.dwSatelliteCount; index++)
            {
                Satellite found = null;
                for (int viewIndex = 0; viewIndex < inViewSatellites.Length && found == null; viewIndex++)
                {
                    if (position.rgdwSatellitesUsedPRNs[index] == inViewSatellites[viewIndex].Id)
                    {
                        found = inViewSatellites[viewIndex];
                        list.Add(found);
                    }
                }
            }

            return (Satellite[])list.ToArray(typeof(Satellite));
        }
        /// <summary>
        /// True if the SatellitesInSolution property is valid, false if invalid
        /// </summary>
        public bool SatellitesInSolutionValid
        {
            get { return (position.dwValidFields & GPS_VALID.SATELLITES_USED_PRNS) != 0; }
        }



        /// <summary>
        /// Satellites in view
        /// </summary>
        /// <returns>Array of Satellites</returns>
        public Satellite[] GetSatellitesInView()
        {
            Satellite[] satellites = null;
            if (position.dwSatellitesInView != 0)
            {
                satellites = new Satellite[position.dwSatellitesInView];
                for (int index = 0; index < satellites.Length; index++)
                {
                    satellites[index] = new Satellite();
                    satellites[index].Azimuth = position.rgdwSatellitesInViewAzimuth[index];
                    satellites[index].Elevation = position.rgdwSatellitesInViewElevation[index];
                    satellites[index].Id = position.rgdwSatellitesInViewPRNs[index];
                    satellites[index].SignalStrength = position.rgdwSatellitesInViewSignalToNoiseRatio[index];
                }
            }

            return satellites;
        }
        /// <summary>
        /// True if the SatellitesInView property is valid, false if invalid
        /// </summary>
        public bool SatellitesInViewValid
        {
            get { return (position.dwValidFields & GPS_VALID.SATELLITES_IN_VIEW) != 0; }
        }


        /// <summary>
        /// Number of satellites used in solution
        /// </summary>
        public int SatelliteCount
        {
            get { return position.dwSatelliteCount; }
        }
        /// <summary>
        /// True if the SatelliteCount property is valid, false if invalid
        /// </summary>
        public bool SatelliteCountValid
        {
            get { return (position.dwValidFields & GPS_VALID.SATELLITE_COUNT) != 0; }
        }

        /// <summary>
        /// Number of satellites in view.  
        /// </summary>
        public int SatellitesInViewCount
        {
            get { return position.dwSatellitesInView; }
        }
        /// <summary>
        /// True if the SatellitesInViewCount property is valid, false if invalid
        /// </summary>
        public bool SatellitesInViewCountValid
        {
            get { return (position.dwValidFields & GPS_VALID.SATELLITES_IN_VIEW) != 0; }
        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?