2013-01-07 72 views
2

我正在尝试确定处理多个地图的最佳方法。我应该从一个地图中添加和删除东西,还是在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()尝试,但我得到同样的结果。

编辑: 这显然不是很容易解决的问题。我环顾四周,我无法找到解决方案。现在我将使用添加/删除来控制我的片段。

这仍然没有解决....我采取了不同的路线。显示/隐藏片段不适用于新的谷歌地图片段的多个实例。我不确定为什么,如果有人发现此问题的解决方案或原因,请发布并分享。

回答

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(我的意思是一个mapfragmentActivity),这个问题,至少在我的情况下,是由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; 

其中Map1FragmentMap2Fragment,他们每个人,extends Fragment和膨胀包含<fragment android:id="@+id/map_fragment1" class="com.google.android.gms.maps.SupportMapFragment" />一个XML布局,但你可以奥斯陆刚好延伸SupportMapFragment,取决于您的需求。