如何在片段中获取Google地图对象 [英] How to get Google Maps object inside a Fragment

查看:119
本文介绍了如何在片段中获取Google地图对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了片段,我是新的。有一个片段,我想要显示Google Map并想要获取它的对象,为此,我在XML中有一个片段,并且我想在片段本身中使其膨胀。当我试图膨胀地图视图时,它显示我的getSupportFragmentManager()错误。



如何获取地图对象,那是主要问题。我的XML是这样的: -

 < fragment 
android:id =@ + id / map
android:layout_width =match_parent
android:layout_height =250dp
class =com.google.android.gms.maps.SupportMapFragment/>

我想要获取Google Map对象的Fragment就像这样: -

  public class FindMyCar extends Fragment {

private TextView mTvFind;
私人TextView mTvPark;
private EditText mEtParkHint;

私人GoogleMap地图;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}


@覆盖
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){

View view = inflater.inflate(R.layout.findmycar,null);

initViews(view);

Typeface Type = Typeface.createFromAsset(getActivity()。getResources()。getAssets(),Multicolore.otf);
mTvFind.setTypeface(type);
mTvPark.setTypeface(type);
mEtParkHint.setTypeface(type);

SupportMapFragment fm =(SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map);


返回视图;
}


@Override
public void onDestroyView(){
super.onDestroyView();
}


@Override
public void onDestroy(){
super.onDestroy();



private void initViews(View view){


mTvPark =(TextView)view.findViewById(R.id。 tvFindCarPark);
mTvFind =(TextView)view.findViewById(R.id.tvFindCar);
mEtParkHint =(EditText)view.findViewById(R.id.etParkHint);
}

有人可以帮助我获取地图的对象,以便我可以显示当前位置并在那里绘制标记。

任何帮助都是可观的。
在此先感谢。

解决方案

试试这个

  GoogleMap map =((SupportMapFragment)getFragmentManager()
.findFragmentById(R.id.map))。getMap();

和在您的xml中

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


I am using fragments in my application and I am new for this. There is a fragment in which I want to display Google Map and want to get its object, for this I have a fragment in my XML and I want to inflate it in the Fragment itself. When I am trying to inflate the map view its showing me error for getSupportFragmentManager().

How do I get the map object then that is the main issue. My XML is like this :-

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

And my Fragment in which I want to get the Google Map object is like that :-

public class FindMyCar extends Fragment {

    private TextView mTvFind;
    private TextView mTvPark;
    private EditText mEtParkHint;

    private GoogleMap map;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


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

        View view = inflater.inflate(R.layout.findmycar, null);

        initViews(view);

        Typeface type = Typeface.createFromAsset(getActivity().getResources().getAssets(),"Multicolore.otf"); 
        mTvFind.setTypeface(type);
        mTvPark.setTypeface(type);
        mEtParkHint.setTypeface(type);

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void initViews(View view){


        mTvPark = (TextView) view.findViewById(R.id.tvFindCarPark);
        mTvFind = (TextView) view.findViewById(R.id.tvFindCar);
        mEtParkHint = (EditText) view.findViewById(R.id.etParkHint);
    }

Can someone help me to get the object for my map so that I can show current location and draw the marker there.

Any help would be appreciable. Thanks in advance.

解决方案

Try this

 GoogleMap map = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

and in your xml

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

这篇关于如何在片段中获取Google地图对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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