2013-01-07 123 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,取決於您的需求。