SupportMapFragment.getmap()返回null [英] SupportMapFragment.getmap() returns null

查看:136
本文介绍了SupportMapFragment.getmap()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载SupportMapFragment动态的片段,这是我的onCreateView()方法:

I'm trying to load SupportMapFragment dynamically in a fragment, here is my onCreateView() method:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View contentView = inflater.inflate(R.layout.frag_map_layout, null);
            shopDtos=ShopListFragment.shopDtos;
            fragment =SupportMapFragment.newInstance();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.map_content, fragment);
            ft.commit();
            map = fragment.getMap();
            mapMarker = map.addMarker(new MarkerOptions().position(new LatLng(0, 0))
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.maps_pin)));
        return contentView;
    }

不幸的是,我得到的GoogleMap的地图为。 到如何动态地创建一个mapfragment任何建议?

Unfortunately, I get the GoogleMap map as null. Any suggestions on to how create a mapfragment dynamically?

推荐答案

地图需要一些时间来加载,所以你需要在处理程序运行code - >

map takes some time to load, so you need to run your code in handler -->

Handler handler = new Handler();
handler.postDelayed(new Runnable() 

   @Override
   public void run() {
       GoogleMap googleMap = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true)).getMap();
       FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.map_content, fragment);
        ft.commit();
       if(googleMap != null) {
          googleMap.addMarker(new MarkerOptions().position(result)).setVisible(true);

          // Move the camera instantly to location with a zoom of 15.
          googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(result, 15));

          // Zoom in, animating the camera.
          googleMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);

          googleMap.getUiSettings().setZoomControlsEnabled(false);
          googleMap.getUiSettings().setCompassEnabled(false);
          googleMap.getUiSettings().setMyLocationButtonEnabled(false);

          handler.removeCallbacksAndMessages(null);
       }

       else {
            handler.postDelayed(this, 500);
       }
 }, 500);

这篇关于SupportMapFragment.getmap()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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