Using SupportMapFragment instead of MapFragment
Asked Answered
F

1

1

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

Forcemeat answered 12/5, 2014 at 8:43 Comment(3)
You're correct - you need to use getSupportFragmentManager() and use that to get the SupportMapFragment.Excursive
Can you please have a look at my edit?Forcemeat
I've never used Xamarin but it looks like things are a bit different there, e.g. there's no getSupportFragmentManager(). This link might help forums.xamarin.com/discussion/14731/… - I'd start with this: Android.Support.V4.App.FragmentManager.Excursive
M
1

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();
    }
}
Massage answered 12/5, 2014 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.