2013-10-16 40 views
3

我能夠使用片段和viewpager在標籤佈局中加載Google地圖。它適用於縱向和橫向。當我第一次加載應用程序,當我切換到地圖選項卡,地圖加載,相機動畫在我的標記已經預先定義拉特和長,我的當前位置啓用。但是,如果我更改標籤,然後返回到地圖選項卡,位置查找程序按鈕不再顯示(左上角按鈕),則攝像機不會爲設置的標記生成動畫,因爲標記已消失,地圖顯示非洲。使用片段更改爲橫向時的地圖行爲更改

這是我的地圖選項卡的代碼。

TabTwo.java

public class TabTwo extends Fragment { 

private static View view; 
/** 
* Note that this may be null if the Google Play services APK is not 
* available. 
*/ 

private static GoogleMap map; 
private static Double latitude, longitude; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (container == null) { 
     return null; 
    } 
    view = inflater.inflate(R.layout.activity_tab_two, container, false); 
    // Passing harcoded values for latitude & longitude. Please change as per your need. This is just used to drop a Marker on the Map 
      latitude = 14.6353475; 
      longitude = 121.0327501; 

      setUpMapIfNeeded(); // For setting up the MapFragment 

    return view; 
} 

/***** Sets up the map if it is possible to do so *****/ 
public static void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (map == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     map = ((SupportMapFragment) MainActivity.fragmentManager 
       .findFragmentById(R.id.map)).getMap(); 
     // Check if we were successful in obtaining the map. 
     if (map != null) 
      setUpMap(); 
    } 
} 

/** 
* This is where we can add markers or lines, add listeners or move the 
* camera. 
* <p> 
* This should only be called once and when we are sure that {@link #mMap} 
* is not null. 
*/ 
private static void setUpMap() { 
    // For showing a move to my loction button 
    map.setMyLocationEnabled(true); 
    // For dropping a marker at a point on the Map 
    map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Estuar Building").snippet("Work Place")); 
    // For zooming automatically to the Dropped PIN Location 
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, 
      longitude), 12.0f)); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    if (map != null) 
     setUpMap(); 

    if (map == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     map = ((SupportMapFragment) MainActivity.fragmentManager 
       .findFragmentById(R.id.map)).getMap(); 
     // Check if we were successful in obtaining the map. 
     if (map != null) 
      setUpMap(); 
    } 
} 

/**** The mapfragment's id must be removed from the FragmentManager 
**** or else if the same it is passed on the next time then 
**** app will crash ****/ 
@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    try { 
     Fragment fragment = (getFragmentManager().findFragmentById(R.id.map)); 
     FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
     ft.remove(fragment); 
     ft.commit(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public void onDetach() { 
    super.onDetach(); 
    try { 
     Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager"); 
     childFragmentManager.setAccessible(true); 
     childFragmentManager.set(this, null); 
    } catch (NoSuchFieldException e) { 
     throw new RuntimeException(e); 
    } catch (IllegalAccessException e) { 
     throw new RuntimeException(e); 
    } 
} 

} 

需要幫助。謝謝!

+0

我還發現我鎖定了我的設備,位置查找程序按鈕消失。 – Jeongbebs

+0

你如何獲得當前位置?我沒有看到你的正確代碼。這就是你看到海洋的原因。另外,當你第二次點擊標籤時,你確定你的方法被調用嗎?你有沒有放置斷點? – fasteque

+0

我使用這個函數,所以我可以檢查Google Map是否知道我的位置「map.setMyLocationEnabled(true);」 – Jeongbebs

回答

1

地圖顯示非洲=地圖顯示GPS(0,0)。無處不在,永遠。 :)

你的setUpMapsIfNeeded是越野車。您在UI上擁有的地圖對象不是地圖變量中的地圖。你必須總是重新填充所有UI變量onCreateView

當片段未顯示時,Android會重新創建您的視圖。視圖上的地圖對象(並且對用戶可見)不是變量中的地圖對象。

如果你不叫

map = ((SupportMapFragment)MainActivity.fragmentManager.findFragmentById(R.id.map)).getMap(); 

每次你創建onCreateView一個新的觀點,你不會有正確的地圖,所有的設置代碼將無法正常工作。

+0

但我只有一個片段的地圖。我的地圖上的地圖與地圖變量中的地圖有何不同? – Jeongbebs

+0

因爲片段沒有顯示幾秒鐘。切換標籤頁時,Android會破壞您的用戶界面。 – meredrica

+0

所以在'onDestroy'中,我必須再次調用'setUpMapIfNeeded'? – Jeongbebs