我正在尝试确定处理多个地图的最佳方法。我应该从一个地图中添加和删除东西,还是在2个地图片段之间切换。 理想情况下,我认为用碎片工作会更好,因为我可以轻松使用后退按钮。多个地图的最佳做法
当我尝试在buttonclick上显示()和隐藏()多个地图片段时,出现问题。 地图不会使用show()/ hide()更改。也许有人可以阐明为什么会发生这种情况,所以我可以更好地了解显示和隐藏的mapviews究竟发生了什么。 有没有人有任何显示和隐藏地图片段的问题?
当我点击按钮时,mapview不会改变,但我失去控制,除非我再次点击按钮。看起来片段正在切换,但并未实际改变其视图。我相信它可以正确使用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();
}
}
编辑:我发现this链接,说明如何显示和隐藏的地图碎片和我一直在使用getView().setVisibility()
尝试,但我得到同样的结果。
编辑: 这显然不是很容易解决的问题。我环顾四周,我无法找到解决方案。现在我将使用添加/删除来控制我的片段。
这仍然没有解决....我采取了不同的路线。显示/隐藏片段不适用于新的谷歌地图片段的多个实例。我不确定为什么,如果有人发现此问题的解决方案或原因,请发布并分享。