我正在尝试确定处理多个地图的最佳方式。我应该在单个地图中添加和删除内容,还是在两个地图片段之间切换。 理想情况下,我认为使用片段会更好,因为我可以轻松使用后退按钮。
当我尝试在buttonclick上显示()和隐藏()多个地图片段时出现问题。 使用show()/ hide()不会更改地图。也许有人可以为我解释为什么会发生这种情况,这样我就可以更好地了解显示和隐藏的地图视图的实际情况。 有人在显示和隐藏地图片段方面有任何问题吗?
当我点击按钮时,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()
,但我得到了相同的结果。
编辑: 这显然不是容易解决的问题。我环顾四周,我无法找到解决方案。现在我将使用添加/删除来控制我的片段。
这仍然没有解决....我采取了不同的路线。显示/隐藏片段不适用于新谷歌地图片段的多个实例。我不确定为什么,如果有人找到解决方案或理由,请发布并分享。
答案 0 :(得分:2)
面对这个问题,在搜索解决方案后,我找到了一些有用的链接:
Initialize MapFragment programmatically with Maps API v2
Multiple Map fragment in action bar tab
Multiple maps v2 in TabActivity
https://stackoverflow.com/questions/15654321/multiple-map-fragment-in-android-app
Issue while using 2 map fragments in One application
Only first map is showing with Multiple SupportMapFragment in ViewPager
Performance of multiple MapFragments (Android Map API v2)
但我决定以我的风格做这件事:
如果片段属于同一个Activity
,这就是我浏览多个地图片段的方式。如果mapfragments
属于不同的Activity
(我的意思是每mapfragment
一个Activity
),这个问题,至少在我的情况下,是由Android操作系统管理的,所以我不应该做任何特别的事。
case 1:
if(getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment1) == null){
if(getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment2) != null){
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.remove(getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment2));
ft.commit();
}
FragmentManager manager1 = getActivity().getSupportFragmentManager();
FragmentTransaction ft1 = manager1.beginTransaction();
ft1.replace(R.id.frame_mapView, new Map1Fragment());
ft1.commit();
}
break;
case 3:
if(getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment2) == null){
if(getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment1) != null){
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.remove(getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment1));
ft.commit();
}
FragmentManager manager1 = getActivity().getSupportFragmentManager();
FragmentTransaction ft1 = manager1.beginTransaction();
ft1.replace(R.id.frame_mapView, new Map2Fragment());
ft1.commit();
}
break;
Map1Fragment
和Map2Fragment
,其中每个extends Fragment
并且包含<fragment
android:id="@+id/map_fragment1"
class="com.google.android.gms.maps.SupportMapFragment"
/>
的XML布局,但您可以只扩展SupportMapFragment
,取决于你的需求。