Trying to put together a little MapFragment in an activity I'm building, but am having some trouble getting it all to work. I know that the Maps api and Play services are both installed correctly, as I did the test tutorial and everything worked fine.
Following the documentation here, I'm running into the following problem: In my setUpMapIfNeeded method, I can either use getFragmentManager()
or getSupportFragmentManager()
. When I use the getFragmentManager()
, Eclipse is cool with it but when I run, I get a NoSuchMethodError
saying that the method is undefined. When I opt for getSupportFragmentManager()
, Eclipse doesn't like it and gives me the error "Cannot cast from Fragment to MapFragment". So what's the deal? Any suggestions?
private void setUpMapIfNeeded() {
//Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) {
//do things to the map
mMap.addMarker(new MarkerOptions().position(LOCATION).title(EXTRA_URL));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LOCATION,15));
mMap.getUiSettings().setZoomControlsEnabled(false);
}
}
}
Let me know if there's any other code I can provide and I'll be happy to post it quickly.
I can either use getFragmentManager() or getSupportFragmentManager().
There should be no debate here. If getSupportFragmentManager()
is available to you, then you are using the Android Support package's backport of fragments, and this is the method that you must use.
When I opt for getSupportFragmentManager(), Eclipse doesn't like it and gives me the error "Cannot cast from Fragment to MapFragment".
That is because you should not be using MapFragment
. You are using the Android Support package's backport of fragments, and therefore you must use SupportMapFragment
.
This is what i had to do, because i was working under level 11;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.support.v4.app.FragmentActivity;
public class MapaActivity extends FragmentActivity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapa)).getMap();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With