我正在尝试确定处理多张地图的最佳方式。我应该在单个地图中添加和删除东西还是在 2 个地图片段之间切换。理想情况下,我认为使用片段会更好,因为我可以轻松使用后退按钮。
当我尝试在 buttonclick 上显示()和隐藏()多个地图片段时,我的问题就出现了。使用 show()/hide() 不会更改地图。也许有人可以解释为什么会发生这种情况,这样我就可以更好地理解地图视图显示和隐藏的实际情况。有人对显示和隐藏地图片段有任何问题吗?
当我单击按钮时,地图视图不会改变,但除非我再次单击按钮,否则我会失去控制。看起来片段正在切换,但实际上并没有改变它的视图。我确信它可以使用 replace() 正常工作,但这违背了加载地图的目的。
package com.test.googletestmaps;
public class ControlFragment extends SherlockFragmentActivity implements
OnClickListener {
private GoogleMap mMap;
private GoogleMap mMap2;
private SupportMapFragment gmap;
private SupportMapFragment gmap2;
private ImageButton button;
private int mapShown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control_fragment);
gmap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map1));
gmap2 = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2));
if (savedInstanceState == null) {
// First incarnation of this activity.
gmap.setRetainInstance(true);
} else {
// Reincarnated activity. The obtained map is the same map instance
// in the previous
// activity life cycle. There is no need to reinitialize it.
mMap = gmap.getMap();
}
if (savedInstanceState == null) {
// First incarnation of this activity.
gmap2.setRetainInstance(true);
} else {
// Reincarnated activity. The obtained map is the same map instance
// in the previous
// activity life cycle. There is no need to reinitialize it.
mMap2 = gmap2.getMap();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.hide(gmap2);
ft.commit();
button = ((ImageButton) findViewById(R.id.cacbutton));
button.setOnClickListener(this);
button.setVisibility(View.VISIBLE);
mapShown = 0;
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map1)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap(0);
}
}
if (mMap2 == null) {
mMap2 = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2)).getMap();
if (mMap2 != null) {
setUpMap(1);
}
}
}
private void setUpMap(int mapNumber) {
switch (mapNumber) {
case 0:
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.addMarker(new MarkerOptions().position(new LatLng(5, 5)).title("Marker for map1"));
break;
case 1:
mMap2.getUiSettings().setZoomControlsEnabled(true);
mMap2.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker for map2"));
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu with the options to show the Map and the ListView.
getSupportMenuInflater()
.inflate(R.menu.activity_control_fragment, menu);
return true;
}
@Override
public void onClick(View v) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
if (mapShown == 0)
{
ft.hide(gmap);
ft.show(gmap2);
mapShown = 1;
}
else
{
ft.hide(gmap2);
ft.show(gmap);
mapShown = 0;
}
ft.addToBackStack(null);
ft.commit();
}
}
编辑:我找到了这个链接,它解释了如何显示和隐藏地图片段,我尝试过使用getView().setVisibility()
,但我得到了相同的结果。
编辑:这显然不是一件容易解决的事情。我环顾四周,找不到解决方案。现在我将使用添加/删除来控制我的片段。
这仍然没有解决....我采取了不同的路线。显示/隐藏片段不适用于新谷歌地图片段的多个实例。我不确定为什么,如果有人找到此问题的解决方案或原因,请发布和分享。