【问题标题】:best practice for multiple maps多张地图的最佳实践
【发布时间】:2012-12-21 18:49:18
【问题描述】:

我正在尝试确定处理多张地图的最佳方式。我应该在单个地图中添加和删除东西还是在 2 个地图片段之间切换。 理想情况下,我认为使用片段会更好,因为我可以轻松地使用后退按钮。

当我尝试在 buttonclick 上显示()和隐藏()多个地图片段时,我的问题出现了。 使用 show()/hide() 不会更改地图。也许有人可以解释为什么会发生这种情况,这样我就可以更好地了解地图视图显示和隐藏的实际情况。 有人对显示和隐藏地图片段有任何问题吗?

当我单击按钮时,地图视图不会改变,但除非我再次单击按钮,否则我会失去控制。看起来片段正在切换,但实际上并没有改变它的视图。我确信它可以使用 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(),但我得到了相同的结果。

编辑: 这显然不是一件容易解决的事情。我环顾四周,找不到解决方案。现在我将使用添加/删除来控制我的片段。

这仍然没有解决....我采取了不同的路线。显示/隐藏片段不适用于新谷歌地图片段的多个实例。我不确定为什么,如果有人找到此问题的解决方案或原因,请发布和分享。

【问题讨论】:

    标签: android google-maps-android-api-2


    【解决方案1】:

    面对这个问题,在寻找解决方案后,我发现了一些有用的链接:

    Initialize MapFragment programmatically with Maps API v2

    Multiple Map fragment in action bar tab

    Multiple maps v2 in TabActivity

    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。如果map fragments 属于不同的Activity(我的意思是每个Activity 一个MapFragment),至少在我的情况下,这个问题是由Android OS 管理的,所以我不应该做任何特别的事情。

               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 并膨胀一个包含

    的 XML 布局
    <fragment
            android:id="@+id/map_fragment1"
            class="com.google.android.gms.maps.SupportMapFragment"
            />
    

    但您也可以仅扩展 SupportMapFragment,这取决于您的需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2011-11-29
      • 2021-10-07
      • 2010-10-11
      • 2015-12-10
      相关资源
      最近更新 更多