首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展MapFragment Android

扩展MapFragment Android
EN

Stack Overflow用户
提问于 2013-12-07 10:06:20
回答 1查看 3.5K关注 0票数 4

我有一个托管两个片段的活动。一个片段向用户提供各种字段中的一些信息。另一个片段应该显示一个Map。

对于托管地图的片段,我需要能够添加自定义逻辑来显示标记和其他几个我想在应用程序中的其他地方重用的东西。在不定义xml片段的情况下创建映射的最佳方法是什么,例如:

代码语言:javascript
复制
<fragment
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

谢谢,内森

编辑:

我现在使用的是嵌套片段,不确定这是否是处理这个问题的最佳方法?我的mapfragment现在是:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

片段代码是:

代码语言:javascript
复制
public class ViewCurrentMapFragment extends Fragment implements View.OnClickListener {
    public static final String TAG = "MAP_FRAGMENT";

    private GoogleMap map;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_view_current_map, container, false);
        return rootView;
    }

    private void getCurrentMarkers() {
        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment smf = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));

        map = smf.getMap();

    }

    @Override
    public void onClick(View v) {
    }


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

}

我现在遇到的问题是smf.getMap()返回null。有什么想法吗?

Edit2:

通过更改以下内容使其生效:

代码语言:javascript
复制
com.google.android.gms.maps.MapFragment

to

com.google.android.gms.maps.SupportMapFragment
EN

回答 1

Stack Overflow用户

发布于 2013-12-07 10:15:29

也许:

代码语言:javascript
复制
<fragment
    class="com.mycompany.MyCustomFragmentExtendingMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

或者像下面这样的嵌套片段:http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1

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

https://stackoverflow.com/questions/20436691

复制
相关文章

相似问题

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