我正在嘗試確定處理多個地圖的最佳方法。我應該從一個地圖中添加和刪除東西,還是在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()
嘗試,但我得到同樣的結果。
編輯: 這顯然不是很容易解決的問題。我環顧四周,我無法找到解決方案。現在我將使用添加/刪除來控制我的片段。
這仍然沒有解決....我採取了不同的路線。顯示/隱藏片段不適用於新的谷歌地圖片段的多個實例。我不確定爲什麼,如果有人發現此問題的解決方案或原因,請發佈並分享。