我能夠使用片段和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);
}
}
}
需要幫助。謝謝!
我還發現我鎖定了我的設備,位置查找程序按鈕消失。 – Jeongbebs
你如何獲得當前位置?我沒有看到你的正確代碼。這就是你看到海洋的原因。另外,當你第二次點擊標籤時,你確定你的方法被調用嗎?你有沒有放置斷點? – fasteque
我使用這個函數,所以我可以檢查Google Map是否知道我的位置「map.setMyLocationEnabled(true);」 – Jeongbebs