我想将谷歌地图嵌入我的应用程序。
但该应用只显示缩放按钮,我看不到地图。 当我切换到其他应用程序并切换回来。地图显示!
我想知道我的应用程序的问题是什么?如何解决?
这就是我的所作所为:
public final static int HANDLER_SHOW_GOOGLEMAP = 4;
public final static int HANDLER_HIDE_GOOGLEMAP = 5;
private final int ENUM_GOOGLE_FRAGMENT_ADD = 0;
private final int ENUM_GOOGLE_FRAGMENT_SHOW = 1;
private final int ENUM_GOOGLE_FRAGMENT_HIDE = 2;
private WeakReference<MyActivity> mActivity;
private LinearLayout m_mapLayout;
private MapFragment m_mapFragment;
public MyHandler(MyActivity activity) {
super(activity);
this.mActivity = new WeakReference<MyActivity>(activity);
// TODO Auto-generated constructor stub
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case HANDLER_SHOW_GOOGLEMAP:
showGoogleMap();
break;
case HANDLER_HIDE_GOOGLEMAP:
hideGoogleMap();
break;
}
}
private void showGoogleMap() {
initGoogleMap();
m_mapLayout.setVisibility(View.VISIBLE);
googleMapFragmentTransaction(ENUM_GOOGLE_FRAGMENT_SHOW);
MyActivity theActivity = this.mActivity.get();
FrameLayout viewGroup = (FrameLayout) theActivity.getWindow()
.getDecorView();
if (null == viewGroup.findViewById(m_mapLayout.getId())) {
viewGroup.addView(m_mapLayout);
}
}
private void hideGoogleMap() {
initGoogleMap();
m_mapLayout.setVisibility(View.INVISIBLE);
googleMapFragmentTransaction(ENUM_GOOGLE_FRAGMENT_HIDE);
MyActivity theActivity = this.mActivity.get();
FrameLayout viewGroup = (FrameLayout) theActivity.getWindow()
.getDecorView();
if (null != viewGroup.findViewById(m_mapLayout.getId())) {
viewGroup.removeView(m_mapLayout);
}
}
private void initGoogleMap() {
if (null != m_mapLayout) {
return;
}
m_mapFragment = MapFragment.newInstance();
MyActivity theActivity = this.mActivity.get();
m_mapLayout = new LinearLayout(theActivity);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 480);
m_mapLayout.setLayoutParams(lp);
m_mapLayout.setId(100011);
m_mapLayout.setY(200);
googleMapFragmentTransaction(ENUM_GOOGLE_FRAGMENT_ADD);
}
private void googleMapFragmentTransaction(int enumTransaction) {
MyActivity theActivity = this.mActivity.get();
FragmentManager fragmentManager = theActivity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
switch (enumTransaction) {
case ENUM_GOOGLE_FRAGMENT_ADD:
fragmentTransaction.add(m_mapLayout.getId(), m_mapFragment);
break;
case ENUM_GOOGLE_FRAGMENT_SHOW:
fragmentTransaction.show(m_mapFragment);
break;
case ENUM_GOOGLE_FRAGMENT_HIDE:
fragmentTransaction.hide(m_mapFragment);
break;
default:
break;
}
fragmentTransaction.commit();
}
我没有足够的声誉上传图片,这是图片的链接: http://imgdata.hoop8.com/1309/336989570575.png