【问题标题】:android mapfragment programmaticallyandroid mapfragment 以编程方式
【发布时间】:2015-04-28 09:23:03
【问题描述】:

我希望将 Android 地图 v2 插入片段,但我的限制是我不能使用 .xml 文件。当我尝试实例化 MapFragment 时出现问题。代码如下:

public class LocationFragment extends Fragment{

    private static View view;
    private static GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (container == null) {

            return null;
        }
        view = (RelativeLayout) inflater.inflate(R.layout.location_fragment, container, false);

        setUpMapIfNeeded(); // For setting up the MapFragment

        MapFragment mf = new 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 (mMap == null) {

            FragmentManager manager = MapActivity.fragmentManager;
            MapFragment smf = (MapFragment) manager.findFragmentById(R.id.location_map);
            mMap = smf.getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null)
                setUpMap();
        }
    }

有人可以帮帮我吗?

【问题讨论】:

    标签: android google-maps fragment mapfragment


    【解决方案1】:

    如果您使用的是片段,我建议如下:

    public class MapFragment extends Fragment {
    
         private static GoogleMap mMap;
         private static UiSettings mUiSettings;
    
         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              final View rootView = inflater.inflate(R.layout.conduccion_map, container, false);
    
              if (mMap == null)
                 mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment)).getMap();
    
              mMap.setMyLocationEnabled(true);
              mUiSettings = mMap.getUiSettings();
              mUiSettings.setZoomControlsEnabled(false);
              mUiSettings.setCompassEnabled(true);
              mUiSettings.setMyLocationButtonEnabled(true);
    
    
             return rootView;
         }
    }
    

    PagerAdapter 调用这个片段的代码可能是这样的:

    class AppSectionsPagerAdapter extends FragmentStatePagerAdapter {
    
        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    
        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    // Map Fragment Activity
                    return new MapFragment();
    
                default:
                    return null;
            }
        }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 使用 childFragmentManager 是将 SupportMapFragment 放入另一个 Fragment 的关键。谢谢!
    【解决方案2】:

    你真的应该使用getMapAsync()。但是您可以这样做,使用片段事务以编程方式添加地图片段。

    MapFragment smf = MapFragment.newInstance(); manager.beginTransaction() .add(FRAGMENT_CONTAINER_ID, smf) .commit();

    【讨论】:

      猜你喜欢
      • 2012-11-23
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多