【发布时间】:2018-02-13 00:11:39
【问题描述】:
我对 Xamarin 很陌生,所以请不要讨厌...
我想创建一个谷歌地图应用程序,显示所有当前正在飞行的 Vatsim 飞行员(Vatsim 是飞行模拟人的在线网络)。
我在地图上显示带有标记的飞行员(带有自定义图像)。
目前我只在启动时更新一次地图!
private void drawPilots()
{
List<VatsimPilot> pilots = Parser.GetPilots();
var matrix = new Matrix();
foreach (var pilot in pilots)
{
matrix.PostRotate(float.Parse(pilot.Heading.ToString()));
_map.AddMarker(new MarkerOptions()
.SetPosition(new LatLng(pilot.Latitude, pilot.Longitude))
.SetTitle(pilot.Callsign)
.SetIcon(BitmapDescriptorFactory.FromBitmap(
Bitmap.CreateBitmap(_planeImg, 0, 0, _planeImg.Width, _planeImg.Height, matrix, true))));
}
}
这很好用。每个人都在正确的地方......
现在我想每 x 秒更新一次地图。
我想出了这个代码:
private void drawPilots()
{
List<VatsimPilot> pilots;
Task.Factory.StartNew(() =>
{
System.Diagnostics.Debug.WriteLine("Thread Started");
while (true)
{
System.Diagnostics.Debug.WriteLine("Getting data");
pilots = Parser.GetPilots();
var matrix = new Matrix();
RunOnUiThread(() =>
{
_map.Clear();
System.Diagnostics.Debug.WriteLine("Updating Map");
foreach (var pilot in pilots)
{
matrix.PostRotate(float.Parse(pilot.Heading.ToString()));
_map.AddMarker(new MarkerOptions()
.SetPosition(new LatLng(pilot.Latitude, pilot.Longitude))
.SetTitle(pilot.Callsign)
.SetIcon(BitmapDescriptorFactory.FromBitmap(
Bitmap.CreateBitmap(_planeImg, 0, 0, _planeImg.Width, _planeImg.Height, matrix, true))));
}
});
Thread.Sleep(Constants.UPDATE_INTERVALL_TIME);
}
});
}
使用此代码,我在地图上看不到任何标记,但地图非常滞后,几秒钟后应用程序崩溃。 Visual Studio 中的输出窗口不是很有帮助,因为它不断显示有关工作人员的信息。 间隔时间当前设置为 10 秒。
提前感谢您的帮助!
【问题讨论】:
-
能否分享一个基本的演示或更完整的演示来重现此问题?
-
我想我需要查看一些代码来帮助您查明问题,并请附上完整的诊断错误日志。
标签: c# android google-maps xamarin.android google-maps-android-api-2