在使用自定义布局的片段中获取谷歌地图的最佳选择是什么?
我正在尝试将GoogleMapsV2用于其中一个片段(标签)。该片段将有一些按钮和谷歌地图在背景中与标记。因此,视图看起来像http://www.androidtapp.com/wp-content/uploads/2009/08/CardioTrainer-Map.jpg(尽管我会比这简单得多)
谷歌搜索后,广泛地溢出堆栈并试验这是我所理解的 1.我可以在我的片段中使用SupportMapFragment(我尝试过它。但是太糟糕了,我无法获得自定义按钮)
2.我可以使用具有更高SDK(sdk 12)的MapFragment。我不介意失去向后兼容性。 但是这个选项看起来不对,因为我的FragmentPagerAdapter期望我返回一个Fragment而不是MapFragment
看起来没有简单/标准的方法来完成这项工作 我需要一些帮助才能朝着正确的方向前进。我是一个新的自学Android开发人员。如果我错过了这里的基础知识,请耐心等待。如果有帮助,我可以发布代码。但是,我正在寻求帮助,以了解进入的方向而不是直接代码的帮助。谢谢你看
答案 0 :(得分:3)
听起来你想要这样的东西:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map_v2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapFragment" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0px" >
<!-- Lots of fancy layout -->
</RelativeLayout>
</RelativeLayout>
然后在你的SupportMapFragment中这样:
public class MyFragment extends Fragment {
private SupportMapFragment fragment;
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_with_map, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}
@Override
public void onResume() {
super.onResume();
if (map == null) {
map = fragment.getMap();
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
}
另外,值得一提的是......第二次重新加载片段似乎存在错误。这是SupportMapFragment的一个解决方法。见这里:The work around
此处还有一个更好的解决方法,但我还没有尝试过:Option 2