使用Support Library v4在片段中获取对GoogleMap的引用 [英] Getting a Reference to a GoogleMap in a fragment using Support Library v4

查看:121
本文介绍了使用Support Library v4在片段中获取对GoogleMap的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用v4支持库将Google Maps添加到片段,但使用getMap()引用它时遇到了一些麻烦。



m像这样添加片段(以下示例在 http:// developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime ):

  public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
$ b $ if(findViewById(R.id.fragment_container)!= null){

if(savedInstanceState!= null){
return;
}

MapFragmentClass mapFragmentClass = new MapFragmentClass();
mapFragmentClass.setArguments(getIntent()。getExtras());
getSupportFragmentManager()。beginTransaction()
.add(R.id.fragment_container,mapFragmentClass).commit();





$ b

然后在mapFragmentClass I' m试图获取这样的引用:
$ b $ pre $ public class MapFragmentClass extends Fragment {

public static GoogleMap MMAP;
private static SupportMapFragment mapFragment;

@Override
public查看onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundled savedInstanceState){

SupportMapFragment mapFragment = SupportMapFragment.newInstance();
mMap = mapFragment.getMap();

返回inflater.inflate(R.layout.mapfragment,container,false);


$ / code $ / pre>

或者像这样

  @Override 
public void onResume(){
super.onResume();
mMap = mapFragment.getMap();

Log.i(TestMap,setCamera);
}

这两个选项都会返回空值。所以基本上我没有当片段处于​​活动状态时对地图的引用。我甚至试图在地图加载10秒后获得一个引用,但可惜。

为什么它不会返回GoogleMap? var mapFragment总是被填充,从我在onCreateView中实例化的那一刻开始,所以如果填充的话,我应该能够获得一个GoogleMap类型,然后&在那里,正确吗?



我尝试在GoogleMap中加载一个普通的Activity,在布局中使用Fragement,然后在onCreate中,我可以实例化并引用它, 没问题。地图似乎也加载得更快。



如果任何人有任何想法如何得到这个工作,我会非常感激。



编辑:我的mapfragment布局:

 <?xml version =1.0encoding = UTF-8\" >?; 

< fragment xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com / tools
android:id =@ + id / map
android:layout_width =match_parent
android:layout_height =match_parent
android:name =com .google.android.gms.maps.MapFragment
tools:layout =@ layout / main/>


解决方案

在MainActivity的FragmentTransaction中,使用SupportMapFragment代替MapFragmentClass。

  public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main); $()
$ b $ if(findViewById(R.id.fragment_container)!= null){

SupportMapFragment mapFragmentClass = new SupportMapFragment();
getSupportFragmentManager()。beginTransaction()
.add(R.id.fragment_container,mapFragmentClass).commit();
getSupportFragmentManager()。executePendingTransactions();
GoogleMap map = mapFragmentClass.getMap();
//做你需要做的事情:


$ b $ / code>

创建SupportMapFragment的方式只是创建一个新的Fragment实例,但由于您没有使用FragmentTransaction添加它,所以没有任何Fragment的生命周期方法被调用。


I'm trying to add Google Maps to a fragment using the v4 support lib, but I have some trouble using getMap() to reference it.

I'm adding the fragment like so (following the example at http://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime) :

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (findViewById(R.id.fragment_container) != null) {

            if (savedInstanceState != null) {
                return;
            }

            MapFragmentClass mapFragmentClass= new MapFragmentClass ();
            mapFragmentClass.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mapFragmentClass).commit();
        }
    }
}

And in the mapFragmentClass I'm trying to get the reference like this:

public class MapFragmentClass extends Fragment {

    public static  GoogleMap mMap;
    private static SupportMapFragment mapFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        SupportMapFragment mapFragment = SupportMapFragment.newInstance();
        mMap = mapFragment.getMap();

        return inflater.inflate(R.layout.mapfragment, container, false);

}

or like this

@Override
public void  onResume(){
    super.onResume();
    mMap = mapFragment.getMap();

    Log.i("TestMap", "setCamera");
}

Both options come back with a null.. So basically I don't have a reference to the map when the Fragment is active. I even also tried getting a reference 10 seconds after the map was loaded, but alas..

Why won't it return a GoogleMap? The var mapFragment is always filled, from the moment I instantiate it in onCreateView, so if that's filled, I should be able to get a GoogleMap type back right then & there, correct?

I've tried loading in a GoogleMap a normal Activity, using a Fragement in the layout, and there, in onCreate, I can instantiate and reference it, no problem. The map seems to load in faster as well.

If anybody has any idea how to get this working, I'd be much obliged.

Edit: My mapfragment layout:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"
          tools:layout="@layout/main"/>

解决方案

In your FragmentTransaction in MainActivity you should add an instance of SupportMapFragment instead of MapFragmentClass.

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (findViewById(R.id.fragment_container) != null) {

            SupportMapFragment mapFragmentClass= new SupportMapFragment ();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mapFragmentClass).commit();
            getSupportFragmentManager().executePendingTransactions();
            GoogleMap map = mapFragmentClass.getMap();
            // do what you need to do with the map
        }
    }
}

The way you were creating SupportMapFragment was just creating a new Fragment instance but since you didn't add it using a FragmentTransaction none of the lifecycle methods of the Fragment were being called.

这篇关于使用Support Library v4在片段中获取对GoogleMap的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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