William William - 1 month ago 13
Android Question

Setting up GoogleMap in Android app

I'm trying to add a googlemap into my app, but I couldn't get it to work.
I'm working with ViewPager on xamarin btw.
Layout:

...
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.maps.MapFragment" />
...


My fragment:

public class InfoFragment : FragmentV4, IOnMapReadyCallback
{
private GoogleMap mMap;

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.InfoFragment, container, false);

//Set up google maps coords in infos section
SetUpMap();
return view;
}

private void SetUpMap()
{
if (mMap == null) {
//((MapFragment)FragmentManager.FindFragmentById (Resource.Id.map)).GetMapAsync (this);

var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map);

mapFragment.GetMapAsync(this);
}
}

//Launching of the actual map
public void OnMapReady (GoogleMap googleMap)
{

//Setup cords and stuff...
}
}


During the setup of my map. When i do

var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map);


it always returns null. can someone help?

Answer

found the problem

android:name="com.google.android.gms.maps.MapFragment" />

should be:

android:name="com.google.android.gms.maps.SupportMapFragment" />

and in my fragment instead of:

var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map);

            mapFragment.GetMapAsync(this);

it should be:

((SupportMapFragment)ChildFragmentManager.FindFragmentById(Resource.Id.map)).GetMapAsync(this);