我一直在制作一个实现FrameLayout的地图,我以编程方式附加MapFragment(我使用FragmentManager添加片段)。这是我的onCreate方法(忽略广告,我只是测试东西):
@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");
}
});
代码正常运行,应用程序启动(Maps API身份验证正常,没有错误),但未绘制地图!只有当我将设备倾斜90度以调用设备配置更改时,地图才会自行绘制并开始为相机设置动画(screenshot)。您可以看到我调用invalidate()
尝试强制片段绘制自己,但它也不起作用(无论如何没有invalidate()
调用都不起作用。)
地图为什么不画自己? :(
答案 0 :(得分:-2)
检查google api密钥,日志中可能会出现警告。