为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ITMISS 2019-11-18 13:13:47
在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    }
查看完整描述

3 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

getMap现在不推荐使用


问题是当对getMap的调用为null时,何时可以重试?


这取决于问题的性质。


如果您在布局中设置SupportMapFragmentvia <fragment>元素,则可以getMap()在中成功调用onCreate()。但是,如果您SupportMapFragment通过构造函数创建via,那还为时过早- GoogleMap尚不存在。您可以扩展SupportMapFragment和覆盖onActivityCreated(),getMap()然后准备就绪。


但是,getMap()可以还回null了一个更大的问题,如没有安装谷歌播放服务。您可能需要使用类似的方法GooglePlayServicesUtil.isGooglePlayServicesAvailable()来检测这种情况并根据需要进行处理。


查看完整回答
反对 回复 2019-11-18
?
波斯汪

TA贡献1811条经验 获得超4个赞

我最终扩展了SupportMapFragment类并使用了回调。代码在这里:


public class MySupportMapFragment extends SupportMapFragment {


public MapViewCreatedListener itsMapViewCreatedListener;


// Callback for results


public abstract static class MapViewCreatedListener {

    public abstract void onMapCreated();

}


@Override

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

    View view = super.onCreateView(inflater, container, savedInstanceState);

    // Notify the view has been created

    if( itsMapViewCreatedListener != null ) {

        itsMapViewCreatedListener.onMapCreated();

    }

    return view;

}

}

我宁愿使用一个接口并覆盖onAttach(activity)方法,但就我而言,我不希望回调返回我的MainActivity。我希望它返回到Fragment的实例。(GoogleMap本质上是一个片段中的一个片段)我设置了回调并以此方式以编程方式加载了地图。我想在MySupportMapFragment的构造函数中设置itsMapViewCreatedListener,但是不建议使用无参数构造函数。


itsMySupportMapFragment = new MySupportMapFragment();

MapViewCreatedListener mapViewCreatedListener = new MapViewCreatedListener() {

    @Override

    public void onMapCreated() {

        initPlacesGoogleMapCtrl();

    }

};

itsMySupportMapFragment.itsMapViewCreatedListener = mapViewCreatedListener;

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.mapFragmentContainer, itsMySupportMapFragment);

transaction.addToBackStack(null);

transaction.commit();

然后,当我打回电话时,我可以得到地图。没有更多的空!


public void initPlacesGoogleMapCtrl() {

    // Map ready, get it.

    GoogleMap googleMap = itsMySupportMapFragment.getMap();

    // Do what you want...

}


查看完整回答
反对 回复 2019-11-18
?
桃花长相依

TA贡献1860条经验 获得超8个赞

我会评论CommonsWare的答案,但我对此没有足够的代表。无论如何,我也遇到了这样的问题,即getMap()在onActivityCreated中将返回null。我的设置是这样的:我有包含片段的MainActivity。在该片段的onCreateView方法中,我创建了SupportMapFragment,然后通过childFragmentManager将其添加到片段中。我一直引用SupportMapFragment,并希望它能在onActivityCreated中为我提供地图,但没有提供。该问题的解决方案是重写SupportMapFragment的onActivityCreated 而不是父片段。我是在onCreateView中这样做的:


@Override

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

    View v = inflater.inflate(R.layout.fragment_location, container, false);

    mMapFragment = new SupportMapFragment() {

        @Override

        public void onActivityCreated(Bundle savedInstanceState) {

            super.onActivityCreated(savedInstanceState);

            mMap = mMapFragment.getMap();

            if (mMap != null) {

                setupMap();

            }

        }

    };

    getChildFragmentManager().beginTransaction().add(R.id.framelayout_location_container, mMapFragment).commit();

return v;   

}


查看完整回答
反对 回复 2019-11-18
  • 3 回答
  • 0 关注
  • 1069 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信