I've been making a map that implements a FrameLayout to whom I attach a MapFragment programatically (I use FragmentManager to add the fragment). Here's my onCreate method (ignore the ad things, I am just testing stuff):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView av = (AdView) findViewById(R.id.top_banner_ad_MM); // Set the banner ad view
AdRequest ar = new AdRequest.Builder()
.addTestDevice("106E258A55DADC2A54C47969C87A91AC")
.build();
av.loadAd(ar);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// Make the new map fragment (MapFragment = google's class)
MapFragment mf = MapFragment.newInstance();
ft.add(R.id.mapview_frame, mf, "map");
ft.commit();
mf.getMapAsync(new OnMapReadyCallback()
{
@Override
public void onMapReady(GoogleMap googleMap)
{
gMap = googleMap; // Use the googleMap reference in this class
MapFragment mf = (MapFragment) getFragmentManager().findFragmentByTag("map");
ViewGroup.LayoutParams lp = mf.getView().getLayoutParams();
lp.height = (findViewById(R.id.mapview_frame)).getHeight();
lp.width = (findViewById(R.id.mapview_frame)).getWidth();
mf.getView().setLayoutParams(lp);
// Set camera position to Ein Gedi 3, Holon
CameraPosition cp = new CameraPosition.Builder()
.target(addr)
.zoom(9) // Set zoom level to be able to see Tel Aviv
.bearing(0) // Set bearing to North
.build(); // Build
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp)); // Apply position
getFragmentManager().findFragmentByTag("map").getView().invalidate();
Log.w("GETEVENT-MAPINIT", "I just finished the fucking map initialization");
}
});
The code works, and the app launches (Maps API auth works correctly, there are no errors) but the map isn't drawn! Only when I tilt the device 90 degrees to invoke a change in device configuration does the map draw itself and begins animating the camera (screenshot). You can see I call invalidate()
to try and force the fragment to draw itself but it doesn't work either (doesn't work without the invalidate()
call anyway).
Why won't the map draw itself? :(