[英]Map behavior changes when changed to landscape orientation using fragments
我能夠使用片段和viewpager在標簽布局中加載Google Map。 它適用於縱向和橫向。 當我第一次加載應用程序時,當我切換到地圖選項卡時,地圖加載,相機動畫在我的標記上,該標記具有預先定義的lat和long,並且我的當前位置已啟用。 但是,如果我更改標簽,然后返回地圖選項卡,位置查找器按鈕消失(左上方按鈕)相機沒有動畫到我設置的標記,因為標記消失了,地圖顯示非洲。
這是我的地圖選項卡的代碼。
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);
}
}
}
需要幫忙。 謝謝!
地圖顯示非洲=地圖顯示GPS(0,0)。 無處不在,永遠。 :)
你的setUpMapsIfNeeded
是錯誤的。 您在UI上擁有的地圖對象不是您在地圖變量中擁有的地圖。 您必須始終在onCreateView
重新填充所有UI變量
當片段未顯示時,Android會重新創建您的視圖。 視圖上的地圖對象(對用戶可見)不是變量中的地圖對象。
如果你不打電話
map = ((SupportMapFragment)MainActivity.fragmentManager.findFragmentById(R.id.map)).getMap();
每次在onCreateView
創建新視圖時,您都沒有正確的地圖,並且所有設置代碼都不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.