if (serialPort1.IsOpen)
{
#region 新建坐标系
Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
System.Object obj = Activator.CreateInstance(factoryType);
ISpatialReferenceFactory3 spatialReferenceFactory = obj as ISpatialReferenceFactory3;
///新建坐标系GCS WGS-84
//D:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World
ISpatialReference spatialReference = spatialReferenceFactory.
CreateESRISpatialReferenceFromPRJFile(@"D:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj");
///新建坐标系
//F:\yy\内蒙\city_bound\NM_city.prj
ISpatialReference dspatialReference = spatialReferenceFactory.
CreateESRISpatialReferenceFromPRJFile(@"F:\yy\内蒙\city_bound\NM_city.prj");
#endregion
#region 公共参数
string str = serialPort1.ReadLine() + "\r\n";
string[] strarray = str.Split(',');
if (strarray[0].Equals("$GPGGA"))
{
if (strarray[2]!="")
{
string jingdu, weidu, gaocheng;
jingdu = strarray[4].Substring(0, 3) + (double.Parse(strarray[4].Substring(3)) / 60).ToString("###.####");
weidu = strarray[2].Substring(0, 2) + (double.Parse(strarray[2].Substring(2)) / 60).ToString("##.####");
gaocheng = strarray[9] + strarray[10].ToLower();
starttrack = true;
#endregion
#region 进行坐标转换
IPoint point = new PointClass();
point.PutCoords(double.Parse(jingdu), double.Parse(weidu));
//Geometry Interface to do actual project
IGeometry geometry;
geometry = point;
geometry.SpatialReference = spatialReference;
geometry.Project(dspatialReference);
point = geometry as IPoint;
double x;
double y;
point.QueryCoords(out x, out y);
#endregion
#region 在地图上显示GPS点
ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
simpleMarkerSymbol.Color = GetRGBColor(0, 0, 255);
simpleMarkerSymbol.Size = 10;
// ESRI.ArcGIS.Display.ISymbol symbol = simpleMarkerSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic cast.
gpspoint.PutCoords(x, y);
object opoint = simpleMarkerSymbol;
axMapControl1.DrawShape(gpspoint, ref opoint);
#endregion
#region 跟踪轨迹
point.X = x;
point.Y = y;
//If this is the first point of a new line
if (m_Polyline == null)
{
//Create the forms private polyline member and grab hold of the IPolyline interface
m_Polyline = new PolylineClass();
}
//QI for the IPointsCollection interface using the IPolyline interface
object o = Type.Missing;
//object o1 = m_PointCollection.PointCount-1;
m_PointCollection = (IPointCollection)m_Polyline;
m_PointCollection.AddPoint(point, ref o, ref o);
//Refresh the foreground thereby removing any text annotation
axMapControl1.Refresh(esriViewDrawPhase.esriViewForeground, Type.Missing, Type.Missing);
#endregion
#region 输出到txt
if (Singlepoint == true)
{
timer1.Enabled = false;
textBox2.Text = textBox2.Text + "Longitude," + jingdu + ",Latitude," + weidu + ",Elevation," + gaocheng + "\r\n";
MessageBox.Show("Longitude," + jingdu + ",Latitude," + weidu + ",Elevation," + gaocheng);
Singlepoint = false;
}
else
{
textBox2.Text = textBox2.Text + "Longitude," + jingdu + ",Latitude," + weidu + ",Elevation," + gaocheng + "\r\n";
}
#endregion
}
else
{
timer1.Enabled = false;
MessageBox.Show("未接收到GPS信号!");
}
}
{
#region 新建坐标系
Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
System.Object obj = Activator.CreateInstance(factoryType);
ISpatialReferenceFactory3 spatialReferenceFactory = obj as ISpatialReferenceFactory3;
///新建坐标系GCS WGS-84
//D:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World
ISpatialReference spatialReference = spatialReferenceFactory.
CreateESRISpatialReferenceFromPRJFile(@"D:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj");
///新建坐标系
//F:\yy\内蒙\city_bound\NM_city.prj
ISpatialReference dspatialReference = spatialReferenceFactory.
CreateESRISpatialReferenceFromPRJFile(@"F:\yy\内蒙\city_bound\NM_city.prj");
#endregion
#region 公共参数
string str = serialPort1.ReadLine() + "\r\n";
string[] strarray = str.Split(',');
if (strarray[0].Equals("$GPGGA"))
{
if (strarray[2]!="")
{
string jingdu, weidu, gaocheng;
jingdu = strarray[4].Substring(0, 3) + (double.Parse(strarray[4].Substring(3)) / 60).ToString("###.####");
weidu = strarray[2].Substring(0, 2) + (double.Parse(strarray[2].Substring(2)) / 60).ToString("##.####");
gaocheng = strarray[9] + strarray[10].ToLower();
starttrack = true;
#endregion
#region 进行坐标转换
IPoint point = new PointClass();
point.PutCoords(double.Parse(jingdu), double.Parse(weidu));
//Geometry Interface to do actual project
IGeometry geometry;
geometry = point;
geometry.SpatialReference = spatialReference;
geometry.Project(dspatialReference);
point = geometry as IPoint;
double x;
double y;
point.QueryCoords(out x, out y);
#endregion
#region 在地图上显示GPS点
ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
simpleMarkerSymbol.Color = GetRGBColor(0, 0, 255);
simpleMarkerSymbol.Size = 10;
// ESRI.ArcGIS.Display.ISymbol symbol = simpleMarkerSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic cast.
gpspoint.PutCoords(x, y);
object opoint = simpleMarkerSymbol;
axMapControl1.DrawShape(gpspoint, ref opoint);
#endregion
#region 跟踪轨迹
point.X = x;
point.Y = y;
//If this is the first point of a new line
if (m_Polyline == null)
{
//Create the forms private polyline member and grab hold of the IPolyline interface
m_Polyline = new PolylineClass();
}
//QI for the IPointsCollection interface using the IPolyline interface
object o = Type.Missing;
//object o1 = m_PointCollection.PointCount-1;
m_PointCollection = (IPointCollection)m_Polyline;
m_PointCollection.AddPoint(point, ref o, ref o);
//Refresh the foreground thereby removing any text annotation
axMapControl1.Refresh(esriViewDrawPhase.esriViewForeground, Type.Missing, Type.Missing);
#endregion
#region 输出到txt
if (Singlepoint == true)
{
timer1.Enabled = false;
textBox2.Text = textBox2.Text + "Longitude," + jingdu + ",Latitude," + weidu + ",Elevation," + gaocheng + "\r\n";
MessageBox.Show("Longitude," + jingdu + ",Latitude," + weidu + ",Elevation," + gaocheng);
Singlepoint = false;
}
else
{
textBox2.Text = textBox2.Text + "Longitude," + jingdu + ",Latitude," + weidu + ",Elevation," + gaocheng + "\r\n";
}
#endregion
}
else
{
timer1.Enabled = false;
MessageBox.Show("未接收到GPS信号!");
}
}