Code Runner
Code Runner

Reputation: 1028

Wanted to load markers on maps asynchronously in Xamarin.Android

I am loading google map and then adding markers on map it usually takes 4 to 7 seconds to load the map with markers but i want to improve performance of app so first i just want to show the map and in background i want to load markers and apply on map later. i tried so many ways like Task.Run(), Thread(threadStart()),RunonUiThread() nothing is working. RunonUiThread and threadstart is blocking UI and taking more time to load where in Task.Run is not loading the markers on map.

    protected override void OnCreate(Bundle savedInstanceState)
        {
        base.OnCreate(savedInstanceState);
          SetUpMap();
         }


    SetUpMap(){
        if (_map == null)
        {
            Utils.mapFrag = 
         (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
            RunOnUiThread(() =>
                {
             //initializing map which is calling IOnMapReady 
                 asynchronously
                    Utils.mapFrag.GetMapAsync(this); 
                });

        }
        else
        {
            LoadMarkersOnMap(_map);
        }

    }

    public void OnMapReady(GoogleMap googleMap)
    {
        _map = googleMap;
        _map.UiSettings.MapToolbarEnabled = false;
        _map.UiSettings.MyLocationButtonEnabled = false;
        _map.UiSettings.ZoomControlsEnabled = false;
        Utils.map = googleMap;
        System.Threading.Tasks.Task.Run(() =>
        {
            LoadMarkersOnMap(_map);
            if (progressDialog != null && progressDialog.IsShowing)
                progressDialog.Dismiss();
        });
  }

 LoadMarkerOnMap(Googlemap _map){
 RunOnUiThread(()=>{
 //here i m getting the list of markers from the database
 and adding it to map 

});

 }

even i tried without putting runonUithread still it is blocking ui or not loading markers properly. sometimes it load markers some times it do not load all markers.

Upvotes: 1

Views: 283

Answers (1)

Raimo
Raimo

Reputation: 1534

Take a look at the Xamarin docs here: Writing Responsive Applications.

Upvotes: 1

Related Questions