我有一个托管两个片段的活动。一个片段向用户提供各种字段中的一些信息。另一个片段应该显示一个Map。
对于托管地图的片段,我需要能够添加自定义逻辑来显示标记和其他几个我想在应用程序中的其他地方重用的东西。在不定义xml片段的情况下创建映射的最佳方法是什么,例如:
<fragment
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>谢谢,内森
编辑:
我现在使用的是嵌套片段,不确定这是否是处理这个问题的最佳方法?我的mapfragment现在是:
<?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>片段代码是:
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:
通过更改以下内容使其生效:
com.google.android.gms.maps.MapFragment
to
com.google.android.gms.maps.SupportMapFragment发布于 2013-12-07 10:15:29
也许:
<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
https://stackoverflow.com/questions/20436691
复制相似问题