首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TabsFragment和MapFragment

TabsFragment和MapFragment
EN

Stack Overflow用户
提问于 2013-02-28 22:52:36
回答 1查看 1.2K关注 0票数 0

我有一个问题和一个问题。首先我不知道这是不是可能的:

我有一个带有标签的片段活动。在一个选项卡(这是一个片段)上,我有一个映射,其中包含一个隐藏的项目列表。映射被放入该片段的帧中。该列表已下载,按下按钮后将再次可见。在地图上,我有代表这些项目的标记。

我的问题是:当从我放在帧中的片段中使用getMap()时,我总是得到map值为null

这个是可能的吗?如果没有,你有什么建议吗?提前谢谢。

编辑过的

代码语言:javascript
复制
public class MapExplore extends Fragment{
private FrameLayout frame;
    private ListView list;

    private String api_key;

    private GoogleMap map;
    private MapExploreAdapter adapter;

    private SupportMapFragment map_fragment;
@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        api_key = Configuration.get_prefereence_string(getActivity(), "user_activation_key", null);

        adapter = new MapExploreAdapter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.map_explore, null);
        frame = (FrameLayout) view.findViewById(R.id.frameMap);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        setUpMapFragment();
    }

    private void setUpMapFragment(){
        if(map_fragment == null){
            map_fragment = SupportMapFragment.newInstance();
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.add(frame.getId(), map_fragment, Utils.MAP_FRAGMENT_TAG);
            ft.commit();

        }
    }

    private void setUpMap(){
        Log.i("SET UP MAP", "Started");
        if(map == null){
            map = ((SupportMapFragment) map_fragment).getMap();
        }
        if(map != null){
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Utils.SF_LAT, Utils.SF_LON), 13));
        }       
    }
@Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setUpMap();
    }

我用一些代码编辑了我的问题...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-28 23:02:04

如果您可以发布一些代码,那么诊断您的问题将会更容易:

同时,尝试动态创建SupportMap片段,如下所示:

代码语言:javascript
复制
mMapFragment = new SupportMapFragment() {
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                GoogleMap map = mMapFragment.getMap();
                if (map != null) {
                    //Your initialization code goes here
                }
            }
        };

在片段内部使用Mapview的改进方法如下:

代码语言:javascript
复制
public class Yourfragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;
    private Bundle mBundle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
}

并将地图视图放在XML中,如下所示:

代码语言:javascript
复制
 <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

下面是MapView官方文档的链接

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15138534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档