我如何知道在使用SupportMapFragment时就可以使用地图了?

在onCreate方法中,我利用SupportMapFragment来显示地图。


    SupportMapFragment fragment = new SupportMapFragment();

    getSupportFragmentManager().beginTransaction()

            .add(android.R.id.content, fragment).commit();

与此结合,我想添加一个标记。问题是,当对的调用getMap为null时,何时可以重试?是否有我可以注册的事件,或者我的方法本身就是错误的?


    mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();

    if(mMap == null)

        //what do I do here?

该地图实际上正在电话上显示,但是我似乎没有运气来获取添加标记的参考。


更新:


我之所以SupportMapFragment通过构造函数创建via,是因为典型的setContentView崩溃了并且无法正常工作。这使我陷入困境,onCreate因为我当时实际上是在创建方法,因此我无法在该方法中获得引用SupportMapFragment。在进一步的调查中,似乎我的setContentView问题是没有同时将Google Play服务jar和模块/ src设置为整个项目的一部分。完成上述两项操作后,setContentView现在可以正常工作了,我可以通过getMap()期望的方式获得参考。


lots.xml ...


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

<fragment xmlns:android="http://schemas.android.com/apk/res/android"

          android:id="@+id/map"

          android:name="com.google.android.gms.maps.SupportMapFragment"

          android:layout_width="match_parent"

          android:layout_height="match_parent" />

LotsActivity.java ...


public class LotsActivity extends FragmentActivity {

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.lots);


        GoogleMap mMap;

        mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();

        if(mMap == null)

            //this should not occur now

    }


ITMISS
浏览 1071回答 3
3回答

缥缈止盈

getMap现在不推荐使用问题是当对getMap的调用为null时,何时可以重试?这取决于问题的性质。如果您在布局中设置SupportMapFragmentvia <fragment>元素,则可以getMap()在中成功调用onCreate()。但是,如果您SupportMapFragment通过构造函数创建via,那还为时过早- GoogleMap尚不存在。您可以扩展SupportMapFragment和覆盖onActivityCreated(),getMap()然后准备就绪。但是,getMap()可以还回null了一个更大的问题,如没有安装谷歌播放服务。您可能需要使用类似的方法GooglePlayServicesUtil.isGooglePlayServicesAvailable()来检测这种情况并根据需要进行处理。

波斯汪

我最终扩展了SupportMapFragment类并使用了回调。代码在这里:public class MySupportMapFragment extends SupportMapFragment {public MapViewCreatedListener itsMapViewCreatedListener;// Callback for resultspublic abstract static class MapViewCreatedListener {&nbsp; &nbsp; public abstract void onMapCreated();}@Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {&nbsp; &nbsp; View view = super.onCreateView(inflater, container, savedInstanceState);&nbsp; &nbsp; // Notify the view has been created&nbsp; &nbsp; if( itsMapViewCreatedListener != null ) {&nbsp; &nbsp; &nbsp; &nbsp; itsMapViewCreatedListener.onMapCreated();&nbsp; &nbsp; }&nbsp; &nbsp; return view;}}我宁愿使用一个接口并覆盖onAttach(activity)方法,但就我而言,我不希望回调返回我的MainActivity。我希望它返回到Fragment的实例。(GoogleMap本质上是一个片段中的一个片段)我设置了回调并以此方式以编程方式加载了地图。我想在MySupportMapFragment的构造函数中设置itsMapViewCreatedListener,但是不建议使用无参数构造函数。itsMySupportMapFragment = new MySupportMapFragment();MapViewCreatedListener mapViewCreatedListener = new MapViewCreatedListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onMapCreated() {&nbsp; &nbsp; &nbsp; &nbsp; initPlacesGoogleMapCtrl();&nbsp; &nbsp; }};itsMySupportMapFragment.itsMapViewCreatedListener = mapViewCreatedListener;FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();transaction.replace(R.id.mapFragmentContainer, itsMySupportMapFragment);transaction.addToBackStack(null);transaction.commit();然后,当我打回电话时,我可以得到地图。没有更多的空!public void initPlacesGoogleMapCtrl() {&nbsp; &nbsp; // Map ready, get it.&nbsp; &nbsp; GoogleMap googleMap = itsMySupportMapFragment.getMap();&nbsp; &nbsp; // Do what you want...}

桃花长相依

我会评论CommonsWare的答案,但我对此没有足够的代表。无论如何,我也遇到了这样的问题,即getMap()在onActivityCreated中将返回null。我的设置是这样的:我有包含片段的MainActivity。在该片段的onCreateView方法中,我创建了SupportMapFragment,然后通过childFragmentManager将其添加到片段中。我一直引用SupportMapFragment,并希望它能在onActivityCreated中为我提供地图,但没有提供。该问题的解决方案是重写SupportMapFragment的onActivityCreated 而不是父片段。我是在onCreateView中这样做的:@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {&nbsp; &nbsp; View v = inflater.inflate(R.layout.fragment_location, container, false);&nbsp; &nbsp; mMapFragment = new SupportMapFragment() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onActivityCreated(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onActivityCreated(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mMap = mMapFragment.getMap();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mMap != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setupMap();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; getChildFragmentManager().beginTransaction().add(R.id.framelayout_location_container, mMapFragment).commit();return v;&nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP