www.pudn.com > GPSTest.rar > Form1.cs


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.WindowsMobile.Samples.Location; 
 
namespace GPSTest 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        ////GPS设备状态对象 
        //GpsDeviceState device = null; 
        ////GPS位置对象 
        //GpsPosition position = null; 
        ////GPS对象 
        //Gps gps = new Gps(); 
        private EventHandler updateDataHandler; 
        GpsDeviceState device = null; 
        GpsPosition position = null; 
 
        Gps gps = new Gps(); 
 
 
        private void Form1_Load(object sender, System.EventArgs e) 
        { 
            btStopGPS.Enabled = false; 
 
            updateDataHandler = new EventHandler(UpdateData); 
 
            //status.Text = ""; 
 
            //status.Width = Screen.PrimaryScreen.WorkingArea.Width; 
            //status.Height = Screen.PrimaryScreen.WorkingArea.Height; 
 
            gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged); 
            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged); 
        } 
 
        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args) 
        { 
            position = args.Position; 
 
            // call the UpdateData method via the updateDataHandler so that we 
            // update the UI on the UI thread 
            Invoke(updateDataHandler); 
 
        } 
 
        void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args) 
        { 
            device = args.DeviceState; 
 
            // call the UpdateData method via the updateDataHandler so that we 
            // update the UI on the UI thread 
            Invoke(updateDataHandler); 
        } 
 
        void UpdateData(object sender, System.EventArgs args) 
        { 
            if (gps.Opened) 
            { 
                string str = ""; 
                if (device != null) 
                { 
                    str = device.FriendlyName + " " + device.ServiceState + ", " + device.DeviceState + "\n"; 
                } 
 
                if (position != null) 
                { 
                      
                    if (position.LatitudeValid) 
                    { 
                        str += "纬度:\n" + position.DoubleLatitude.ToString() + "\n"; 
                        //str += "Latitude (DD):\n   " + position.Latitude + "\n"; 
                        //str += "Latitude (D,M,S):\n   " + position.LatitudeInDegreesMinutesSeconds + "\n"; 
                    } 
 
                    if (position.LongitudeValid) 
                    { 
                        str += "经度:\n" + position.DoubleLongtitude.ToString() + "\n"; 
                        //str += "Longitude (DD):\n   " + position.Longitude + "\n"; 
                        //str += "Longitude (D,M,S):\n   " + position.LongitudeInDegreesMinutesSeconds + "\n"; 
                    } 
 
                    if (position.SpeedValid) 
                    { 
                        str += "速度:\n" + position.Speed.ToString() + "\n"; 
                    } 
 
                    if (position.HeadingValid) 
                    { 
                        str += "方向:\n" + position.Heading.ToString() + "\n"; 
                    } 
 
                    if (position.SatellitesInSolutionValid && 
                        position.SatellitesInViewValid && 
                        position.SatelliteCountValid) 
                    { 
                        str += "Satellite Count:\n   " + position.GetSatellitesInSolution().Length + "/" + 
                            position.GetSatellitesInView().Length + " (" + 
                            position.SatelliteCount + ")\n"; 
                    } 
 
                    if (position.TimeValid) 
                    { 
                        str += "Time:\n   " + position.Time.ToString() + "\n"; 
                    } 
                } 
 
                status.Text = str; 
 
            } 
        } 
 
        private void Form1_Closed(object sender, System.EventArgs e) 
        { 
            if (gps.Opened) 
            { 
                gps.Close(); 
            } 
        } 
 
        private void stopGpsMenuItem_Click(object sender, EventArgs e) 
        { 
            if (gps.Opened) 
            { 
                gps.Close(); 
            } 
 
            btStarGPS.Enabled = true; 
            btStopGPS.Enabled = false; 
        } 
 
        private void startGpsMenuItem_Click(object sender, EventArgs e) 
        { 
            if (!gps.Opened) 
            { 
                gps.Open(); 
            } 
 
            btStarGPS.Enabled = false; 
            btStopGPS.Enabled = true; 
        } 
 
    } 
}