Reputation: 1675
I have implemented a map into my application though on a single view. After creating a few layouts I wanted to implement a bottom navigation view which meant I had to use fragments. I have already moved my activity's code into my fragment named map_fragment, at first I wasn't sure where to use FindFragmentById but according to findViewById in Fragment this should be used on the OnCreateView() lifecycle method. My main problem now is capturing the map fragment, since I get the error The name 'SuportMapFragment' does not exist in the current context when using
var mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.gmap);
// Was able to use FragmentManager, but it returns null...
// var mapFragment = (SupportMapFragment)FragmentManager.FindFragmentById(Resource.Id.gmap);
mapFragment.GetMapAsync(this);
Here's the full code
public class map_fragment : Android.Support.V4.App.Fragment, IOnMapReadyCallback
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
// Capture the Map Fragment
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
base.OnCreateView(inflater, container, savedInstanceState);
View view = inflater.Inflate(Resource.Layout.activity_main, container, false);
var mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.gmap);
//var mapFragment = (SupportMapFragment)FragmentManager.FindFragmentById(Resource.Id.gmap);
mapFragment.GetMapAsync(this);
return view;
}
public void OnMapReady(GoogleMap _map)
{
// Some other code here
}
}
As requested this is my main_activity
public class MainActivity : AppCompatActivity
{
BottomNavigationView bottomNavigation;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Initialize Xamarin Essentials for the Geolocation Service
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.main_frame);
//var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
//if (toolbar != null)
//{
// SetSupportActionBar(toolbar);
// SupportActionBar.SetDisplayHomeAsUpEnabled(false);
// SupportActionBar.SetHomeButtonEnabled(false);
//}
bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
bottomNavigation.NavigationItemSelected += BottomNavigation_NavigationItemSelected;
// Load the first fragment on creation
LoadFragment(Resource.Id.profile_view);
}
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
void LoadFragment(int id)
{
Android.Support.V4.App.Fragment fragment = null;
switch (id)
{
case Resource.Id.profile_view:
fragment = profile_fragment.NewInstance();
break;
case Resource.Id.map_view:
fragment = map_fragment.NewInstance();
break;
}
if (fragment == null)
return;
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
These are the tutorials I am using as a guide
Upvotes: 0
Views: 114
Reputation: 14991
As you are using the SupportMapFragment
in a Fragment
,you could call it like this:
SupportMapFragment mapFragment = Activity.SupportFragmentManager.FindFragmentById(Resource.Id.gmap).JavaCast<SupportMapFragment>();
if (mapFragment == null)
{
mapFragment = SupportMapFragment.NewInstance();
mapFragment.GetMapAsync(this);
}
refer the SupportMapFragment in your fragment xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gmap"
class="com.google.android.gms.maps.SupportMapFragment" />
update(like this,we could handle OnMapReady
):
SupportMapFragment mapFragment = ChildFragmentManager.FindFragmentById(Resource.Id.gmap).JavaCast<SupportMapFragment>();
mapFragment.GetMapAsync(this);
Upvotes: 2