Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Map in my MapFragment is null

$
0
0

I have a mapfragment. When I initially load the mapfragment, everything is fine. The content is displayed appropriately, my markers load, we're at a good zoom level, everything is correct. I have a set of tabs up in my actionbar. The map is in the default loaded tab. When I select one of the other tabs, I load up a listfragment. The listfragment loads, appropiate data is shown. All is good. When I select the tab to go back to my map fragment, I get a map displayed that is centered at 0,0 (or there abouts) and is at a very high altitude. This looks like the default setting to me for the maps, but that is just a guess. I would like to reload the mapfragment with the previous data and location on it. I use the code shown below to manage the tabs, and this has the TabSelected event within it. I would think that the map would be saved off as part of the mapfragment. Unfortunately, the .Map property of my mapfragment is null when this code executes. I think that the map is set as null because the map fragment is not shown. Questions: 1. The .Map property of the mapfragment is null. is there a way to initialize this property? Do I need to wait until the map loads? How do I know when the map actually loads? There does not seem to be any type of event that fires when a map loads. Should I spin up a 2nd thread to see if a map has loaded and then perform the reset of the map (that seems awfully nasty and wasteful to do)? 2. Am I performing my fragment changes properly? Should I being doing something different from .Remove and .Replace? Is this causing my problems in any way?

    void AddTab (string tabText, int ResourceId)
    {
        var tab = this.ActionBar.NewTab ();            
        //tab.SetText (tabText);
        tab.SetIcon (ResourceId);

        // must set event handler before adding tab
        tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e) {              
            var t = sender as ActionBar.Tab;
                var fragTx = e.FragmentTransaction;
                var frag = FragmentManager.FindFragmentById(Resource.Id.map);
                if ( (frag == _mapFragment) && (_map != null ))
                    {
                    var target = _map.CameraPosition.Target;
                    app.Lat = target.Latitude;
                    app.Lng = target.Longitude;
                    app.Zoom = _map.CameraPosition.Zoom;
                }
                switch(t.Position)
                {
                    case 0:
                        if ( isInitial )
                        {
                            InitMap();
                            alf = new AssignmentListFragment();
                        }
                        else                            
                        {
                            fragTx.Remove(frag);
                        }
                        fragTx.Replace(Resource.Id.map, _mapFragment);
                        isInitial = false;
                    break;
                    case 1:
                        fragTx.Remove(frag);
                        fragTx.Replace(Resource.Id.map, alf);
                        break;
                }
        };
        this.ActionBar.AddTab (tab);
    }

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>