上一篇文章写了小车点的行进路径该有鼠标动态获取(https://blog.csdn.net/qq_27380923/article/details/80080285),那么我这次在改进,让点随着这个地图的蓝点走
第一步:在编程之前,要将 shp文件导入数据库,我用的工具是FWTools,和mysql,使用方法有(https://www.cnblogs.com/mcgaradytien/p/5212551.html)
我将会提供fwtools工具以及我动态追踪的源码(https://download.csdn.net/download/qq_27380923/10373709)
第二步道路数据是这样的
x,y坐标是合在一起,不是很好弄,这里我有两个方法1,直接获取,在剪切2,直接在数据库上处理数据
我用的是第二种
注意,x,y都要double型,因为我们从数据库获得的是object型,要强转;
第三步:
private void MoveCar()
{
//得到当前活动范围
IActiveView pActiveView = axMapControl1.ActiveView;
//开始画笔
pActiveView.ScreenDisplay.StartDrawing(pActiveView.ScreenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache);
IPoint ppoint;
IGeometry pgeo;
IPictureMarkerSymbol psymbol = new PictureMarkerSymbolClass();
IRgbColor prgbcolor = new RgbColorClass();
prgbcolor.Red = 0;
prgbcolor.Green = 0;
prgbcolor.Blue = 0;
psymbol.BitmapTransparencyColor = prgbcolor;
psymbol.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, @"C:\Users\deng\Desktop\毕业设计\NodeAnimation\Command1.bmp");
psymbol.Size = 15;
psymbol.Angle = 270;
//ISimpleMarkerSymbol psimplesymbol = new SimpleMarkerSymbolClass();
//psimplesymbol.Size = 10;
//psimplesymbol.Color = (IColor)prgbcolor;
ppoint = new PointClass();
ppoint.PutCoords(dx, dy);
pgeo = ppoint;
pActiveView.ScreenDisplay.SetSymbol((ISymbol)psymbol);
pActiveView.ScreenDisplay.DrawPoint(ppoint);
//结束画笔
pActiveView.ScreenDisplay.UpdateWindow();
pActiveView.ScreenDisplay.FinishDrawing();
//System.Object obj = psymbol;
//this.axMapControl1.DrawShape(pgeo, ref obj);
//this.axMapControl1.CenterAt(ppoint);
IEnvelope penv = this.axMapControl1.Extent;
penv.CenterAt(ppoint);
this.axMapControl1.Extent = penv;
}
第四步
private void toolStripButton2_Click(object sender, EventArgs e)
{
int a = 0;
MySqlConnection sqlCon = new MySqlConnection("server=localhost;User Id=root;Password=8388056;Database=school");
sqlCon.Open();
String querx = "select x from schoolcar";//向数据库服务器发送指令
MySqlCommand cmd = new MySqlCommand(querx, sqlCon);
//执行结果赋值到dr,dr为只读
MySqlDataReader drx = cmd.ExecuteReader();
while (drx.Read())
{
arrx2[a] = (double)drx[0];
a++;
}
drx.Close();
}
连接数据库获取x坐标
private void toolStripButton4_Click(object sender, EventArgs e)
{
int b = 0;
MySqlConnection sqlCon = new MySqlConnection("server=localhost;User Id=root;Password=8388056;Database=school");
sqlCon.Open();
String query = "select y from schoolcar";//向数据库服务器发送指令
MySqlCommand cmdx = new MySqlCommand(query, sqlCon);
//执行结果赋值到dr,dr为只读
MySqlDataReader dry = cmdx.ExecuteReader();
while (dry.Read())
{
arry2[b] = (double)dry[0];
b++;
}
dry.Close();
}
获得y坐标
第五步
int c = 0;
private void timer2_Tick(object sender, EventArgs e)
{
if (c<227)
{
dx = arrx2[c];
dy = arry2[c];
MoveCar();
c++;
}
else
{
this.timer2.Dispose();
MessageBox.Show("走完了");
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
this.timer2.Enabled = true;
this.timer2.Interval = 1000;
}
double[] arrx2 = new double[1000];
double[] arry2 = new double[1000];