user3548779
user3548779

Reputation: 321

Using SupportMapFragment instead of MapFragment

I am using Xamarin and I have modified the Google Maps API 2 sample to use SupportMapFragment objects rather than MapFragment objects.

Can I have some help with the InitMapFragment function.

Here is the code:

private void InitMapFragment()
{
    _mapFragment = FragmentManager.FindFragmentByTag("map") as SupportMapFragment;
    if (_mapFragment == null)
    {
        GoogleMapOptions mapOptions = new GoogleMapOptions()
            .InvokeMapType(GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(true)
            .InvokeCompassEnabled(true);

        FragmentTransaction fragTx = FragmentManager.BeginTransaction();
        _mapFragment = SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "map");
        fragTx.Commit();
    }
}

_mapFragment used to be of type MapFragment, but now is SupportMapFragment.

Also, the activity is inheriting from Activity at the moment, should this be FragmentActivity, or something else?

Here are the errors that I am getting:

Error CS0039: Cannot convert type 'Android.App.Fragment' to 'Android.Gms.Maps.SupportMapFragment' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

Error CS1503: Argument 2: cannot convert from 'Android.Gms.Maps.SupportMapFragment' to 'Android.App.Fragment'

I am pretty sure that I need to use a SupportFragmentManager rather than a FragmentManager, yet would like some help please.

EDIT

When trying to use SupportFragmentManager, I am getting the following error:

Error CS0103: The name 'SupportFragmentManager' does not exist in the current context

Thanks in advance

Upvotes: 1

Views: 1855

Answers (1)

Nana Ghartey
Nana Ghartey

Reputation: 7927

SupportFramgentManager is inherited from FragmentActivity so make sure the activity extends FragmentActivity. Then modify your code as follows:

private void InitMapFragment()
{
    _mapFragment = SupportFragmentManager.FindFragmentByTag("map") as SupportMapFragment;
    if (_mapFragment == null)
    {
        GoogleMapOptions mapOptions = new GoogleMapOptions()
            .InvokeMapType(GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(true)
            .InvokeCompassEnabled(true);

        FragmentTransaction fragTx = SupportFragmentManager.BeginTransaction();
        _mapFragment = SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "map");
        fragTx.Commit();
    }
}

Upvotes: 1

Related Questions