I'm trying to implement Google Map display and below are my codes:
package com.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MapFragment extends SherlockMapFragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
SupportMapFragment mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
mMap = mapFrag.getMap();
return root;
}
}
Im getting an error that getSupportFragmentManager() is undefined for type MapFragment. However I'm a little confused as I have imported com.google.android.gms.maps.SupportMapFragment. shouldn't this method come from there then? Am I missing out some steps in getting this method to work?
If you are using this SherlockMapFragment, try:
getSherlockActivity().getSupportFragmentManager();
Dude, I spent last couple of hours reading this and other 2 of your questions (among others) because I was having exactly same issue. I had an app working ABS+GMapsV2.0+FragmentActivity, but since I hear a lot that it's a good practice to have it all in fragments, I started porting it to fragments and FragmentActivities as containers.
When I ported the map, happened exactly the same, getSupportFragmentManager() was not recognized, and I did not like quite much the proposed solution creating the SherlockMapFragment messing up the ABS library, so I kept digging.
Most likely you knew already at this point, but I just realize you (as well as me 10 minutes ago) were trying to handle a fragment that is defined in xml but is not created (indexed) yet.
Taking a look at documentation checking the order of calls in fragment methods, I tried the code in other method after onCreateView()... and changing getSupportFragmentManager() for getFragmentManager() or even following the solution given using getSherlockActivity().getSupportFragmentManager() I could handle those fragments which already exist.
public class MapsFragment extends SherlockFragment {
private SupportMapFragment mSupportMapFragment;
private GoogleMap map;
private Marker spotAMarker;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.maps_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_fragment_test); //THIS ONE
mSupportMapFragment = (SupportMapFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment_test); //OR THIS ONE
if(mSupportMapFragment != null) {
map = mSupportMapFragment.getMap();
if(map != null) {
spotAMarker = map.addMarker(new MarkerOptions().position(new LatLng(4.651904,-74.057352)).
title(getString(R.string.spot_a_title)).snippet(getString(R.string.spot_a_subtitle)).
icon(BitmapDescriptorFactory.fromAsset("images/spot_a.png")));
spotAMarker.setDraggable(true);
} else {
Toast.makeText(this.getActivity(), getString(R.string.no_map_error), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this.getActivity(), "No Map Fragment", Toast.LENGTH_LONG).show();
}
super.onActivityCreated(savedInstanceState);
}
}
May be you got already the workaround for the issue, but I wanted to share my experience with other stucked in same issue.