简体   繁体   中英

Xamarin Android Google Maps Update Map Markers Async

I am quite new to Xamarin, so pls no hate...

I want to create a Google Maps app which shows all currently flying Vatsim pilots (Vatsim is an online network for flight simers).

I display the pilots with markers on the map (with an custom image).

Currently I am only updating the map once, on startup!

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))));
    }
}

This works fine. Everyone is in the right place...

Now I want to update the map every x seconds.

I came up with this code:

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);
        }
    });
}

With this code I don't see any markers on the map, however the map is very very laggy and after a few seconds the app crashes. The output window in Visual Studio is not very helpful because it constantly shows infos about the worker. The intervall time is currently set to 10 seconds.

Thanks for your help in advance!

the map is very very laggy and after a few seconds the app crashes.

I think something block your UI thread and causes app crash. When the UI thread of an Android app is blocked for too long, an "Application Not Responding" (ANR) error is triggered.

I want to update the map every x seconds.

You can create a Task which can help you update Map every x seconds:

void ChangedMapData()
{
    Task.Delay(x000).ContinueWith(t =>
    {
       UpdateYourMap();
       ChangedMapData();//This is for repeate every xs.
    }, TaskScheduler.FromCurrentSynchronizationContext());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM