⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 GPS与PC连接通信程序
💻 CS
字号:
// Copyright 2006 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at /arcgis/developerkit/userestrictions.
// 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Mobile.Gps;

namespace MobileGPS
{
  public partial class Form1 : Form
  {
    GpsEventArgs m_gpsea = null;

    public Form1()
    {
      InitializeComponent();
    }

    private void DisplayPosition()
    {
      if (m_gpsea == null)
        return;

      labelLatitude.Text = m_gpsea.Latitude.ToString();
      labelLongitude.Text = m_gpsea.Longitude.ToString();
      labelAltitude.Text = m_gpsea.Altitude.ToString();

      labelFixMode.Text = m_gpsea.FixStatus.ToString();

      labelPDOP.Text = m_gpsea.Pdop.ToString();
      labelVDOP.Text = m_gpsea.Vdop.ToString();
      labelHDOP.Text = m_gpsea.Hdop.ToString();

      DateTime utc = DateTime.MinValue;
      utc = m_gpsea.Time;

      labelDate.Text = utc.ToShortDateString();
      labelTimeLocal.Text = utc.ToLocalTime().ToString();
      labelTime.Text = utc.ToShortTimeString();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      DisplayPosition();
      DisplaySatellites();
    }

    private void serialPortGpsConnection1_GpsChanged(object sender, GpsEventArgs e)
    {
      m_gpsea = e;
    }

    private void DisplaySatellites()
    {
      //labelSatellitesInView.Text = m_gpsea.FixSatelliteCount.ToString();
			//code from Coding4Fun: http://msdn.microsoft.com/coding4fun/someassemblyrequired/whereami/default.aspx


      Pen circlePen = new Pen(System.Drawing.Color.DarkBlue, 1);

      Graphics g = picSats.CreateGraphics();

      int centerX = picSats.Width / 2;
      int centerY = picSats.Height / 2;

      double maxRadius = (Math.Min(picSats.Height, picSats.Width) - 20) / 2;

      //draw circles
      double[] elevations = new double[] { 0, Math.PI / 2, Math.PI / 3, Math.PI / 6 };

      foreach (double elevation in elevations)
      {
        double radius = (double)System.Math.Cos(elevation) * maxRadius;
        g.DrawEllipse(circlePen, (int)(centerX - radius), (int)(centerY - radius), (int)(2 * radius), (int)(2 * radius));
      }
      //90 degrees elevation reticule
      g.DrawLine(circlePen,(centerX - 3), centerY,(centerX + 3), centerY);
      g.DrawLine(circlePen,centerX, (centerY - 3), centerX, (centerY + 3));

      Pen satPen = new Pen(System.Drawing.Color.Green, 4);
 
			listSatellites.Items.Clear();

      try
      {
        foreach (Satellite sat in m_gpsea.GetSatellites())
        {
          if (null == sat) return;

          ListViewItem lvItem = new ListViewItem
            (
              new string[] 
              {
                sat.Id.ToString(),
                sat.Elevation.ToString(),
                sat.Azimuth.ToString(),
                sat.Snr.ToString(),
              }
            );

          listSatellites.Items.Add(lvItem);

          //draw satellites
          double h = (double)System.Math.Cos((sat.Elevation * Math.PI) / 180) * maxRadius;

          int satX = (int)(centerX + h * Math.Sin((sat.Azimuth * Math.PI) / 180));
          int satY = (int)(centerY - h * Math.Cos((sat.Azimuth * Math.PI) / 180));

          g.DrawRectangle(satPen, satX, satY, 4, 4);
          g.DrawString(sat.Id.ToString(), new Font("Verdana", 8, FontStyle.Regular), new System.Drawing.SolidBrush(Color.Black), (satX + 5), (satY + 5));
        }
      }
      catch
      {
        return;
      }
    }


    private void menuItem1_Click(object sender, EventArgs e)
    {
      //softmenu1 gps connect
      serialPortGpsConnection1.Open();
      timer1.Enabled = true;
      menuItem1.Enabled = false;
      menuItem2.Enabled = true;
    }

    private void menuItem2_Click(object sender, EventArgs e)
    {
      //softmenu2 gps disconnect
      timer1.Enabled = false;
      serialPortGpsConnection1.Close();
      menuItem2.Enabled = false;
      menuItem1.Enabled = true;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      if (string.IsNullOrEmpty(mapCache1.Url))
      {
        MessageBox.Show("To use the GPS sample you need to specify, at the design time, a URL of a mobile server that covers your area.", "GPS Sample error - MapCache.URL is empty.");
        this.Close();
        return;
      }

      //initialize
      //serialPortGpsConnection1.PortName = "COM7"; //harcode...
      menuItem2.Enabled = false; //disc gps softkey

      //create the cache and open
      if (mapCache1.Exists)
        mapCache1.Delete();
      mapCache1.Create();

      mapCache1.Open();

    }

    private void map1_ExtentChanged(object sender, EventArgs e)
    {
      if (!map1.IsValid || !mapCache1.IsSynchronizationProtocolValid)
        return;
      mapCache1.GetDataAsync(map1, true, null);
    }

  }
}

⌨️ 快捷键说明

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