带有自定义布局的片段中的Google Map [英] Google Map inside a fragment with custom layout

查看:60
本文介绍了带有自定义布局的片段中的Google Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用自定义布局将Google地图分成一个片段,我的最佳选择是什么?

What is my best option to get google map in a fragment with custom lay out?

我正在尝试将GoogleMapsV2用于片段(选项卡)之一.该片段将具有一些按钮,并且在背景中带有标记的google map.因此该视图看起来像 http://www .androidtapp.com/wp-content/uploads/2009/08/CardioTrainer-Map.jpg (我的比这要简单得多)

I am trying to use GoogleMapsV2 for one of the fragment(tab). The fragment would have some buttons and the google map in the background with markers. So the view would look some thing like http://www.androidtapp.com/wp-content/uploads/2009/08/CardioTrainer-Map.jpg (Mine would be lot simpler than that though)

谷歌搜索后,大量溢出堆栈并进行实验,这是我的理解 1.我可以在片段中使用SupportMapFragment(我尝试过可以使用.但是很遗憾,我无法获得自定义按钮)

After googling ,overflowing the stack extensively and experimenting this is what I understood 1.I can use SupportMapFragment in my fragment (I tried it works. But too bad I am not able to get my custom buttons)

2.我可以将MapFragment与更高的SDK(sdk 12)一起使用.我不介意失去向后兼容性. 但是此选项看起来不对,因为我的FragmentPagerAdapter期望我返回一个Fragment而不是MapFragment

2.I can use MapFragment with higher SDK (sdk 12). I dont mind loosing backward compatibility. But this option looks wrong as my FragmentPagerAdapter expects that I return a Fragment and not MapFragment

似乎没有简单/标准的方法可以完成此任务 我需要一些帮助,朝着正确的方向前进.我是一个新的自学的android开发人员.因此,如果我错过这里的基础知识,请多多包涵.如果有帮助,我可以发布代码.但是,我在寻求理解前进方向方面寻求帮助,而不是直接代码方面寻求帮助.感谢您的关注

Looks like there is no easy/standard method to get this done I need some help in moving in the right direction. I am a new self taught android developer. So bear with me if I miss the basics here. I can post the code if that helps. However I am looking for help in understanding the direction to go in rather than help in direct code. Thanks for looking

推荐答案

听起来像您想要的东西:

Sounds like you want something like this:

<?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中像这样:

And then in your SupportMapFragment something like this:

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的解决方法.参见此处:解决方法

Also, worth mentioning... There appears to be a bug with reloading the fragment for a second time. This is something of a work around for the SupportMapFragment. See here: The work around

这里还有一个更好的解决方法,但是我还没有尝试过:

There is also a possible better fix to this here, but I haven't tried it yet: Option 2

这篇关于带有自定义布局的片段中的Google Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆