[英]How to auto-add item to a list from an incoming serial data?
我正在尝试使用c#和GMap.net跟踪对象的实时位置。 在GMap.net内,为了跟踪路线,我必须使用List<PointLatLng>
,然后使用Pen
统一它们。
问题在于我不知道对象的开始位置,因此我没有要添加的点,一部分没有添加。
是否可以仅将一个点添加到列表中并实时更新?
或者,可以将第一个点保存到变量中,然后使用以前的变量名称添加其他点,每次从串行端口接收到数据时,将数字加1,然后用新的点刷新列表?
`
List<PointLatLng> points = new List<PointLatLng>();
var coordinates = new List<PointLatLng> { new PointLatLng(GPS_Latitude, GPS_Longitude) };
coordinates.Add(new PointLatLng(GPS_Latitude, GPS_Longitude));
GMapOverlay trace = new GMapOverlay("Real Time Position");
GMapRoute carroute = new GMapRoute(coordinates, "Traiectory");
gMapControl1.Overlays.Add(trace);
trace.Routes.Add(carroute);
//Pen redPen = new Pen(Color.Red, 3);
carroute.Stroke = new Pen(Color.Red, 3);
`
这样,我只有第一点,而没有其他。 通过串口以1hz频率接收数据。 我的意思是:即使我收到的数据多于1个,列表也只计一项。
是的 ,这些解决方案都是可行的。
您可以有一个点列表,然后在想向列表中添加点时调用.Add()
(如果只保存一个点,则使用List
毫无意义-如果是这种情况,请参见下一个示例)。 这样,您便拥有了所有要点的历史。 您始终可以通过访问列表中最后一个索引处的项目来获取最新点:
// Create list and (optionally) add the first point to it
var points = new List<Point> { new Point(x, y) };
// Add a new point to the list whenever you want
points.Add(new Point(newX, newY));
// Get the most recent point by getting the item at the last index
var newestPoint = points[points.Count - 1]; // Note: this will fail if the list is empty
您也可以有一个点变量,然后通过独立更改值或为其分配新的点来更新其坐标:
var myPoint = new Point(x, y);
// Update coordinates independently whenever you want
myPoint.X = newX;
myPoint.Y = newY;
// Or just assign a new Point
myPoint = new Point(newX, newY);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.