把地图(谷歌地图V2)的片段里​​最好的方法 [英] Best way to put a map (google maps v2) inside a fragment

查看:162
本文介绍了把地图(谷歌地图V2)的片段里​​最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段(ApartmentFragment),它是示出公寓的细节的页面。我想补充一点的地图显示,其中的公寓。 只是为了清楚起见,我使用的MaterialNavigationDrawer是在GitHub上,我的应用程序是由一个单一的活动(导航抽屉)和许多碎片组成的。

I have a fragment (ApartmentFragment) which is a page that show details of an apartment. I want to add a little map to show where the apartment is. just for clarity, I'm using the MaterialNavigationDrawer that is on github, my app is composed by a single Activity (the navigation drawer) and many fragments.

每一个计算器后我已阅读并没有帮助我。他们中许多人有点混乱。我不想只在ApartmentFragment地图,我想告诉其他的东西加在地图。 这是做到这一点的最好方法是什么? 可以把mapfragment片段里面?或者我需要在活动转化ApartmentFragment?

Every stackoverflow post I have read didn't helped me. Many of them were a bit confusing. I don't want only the map inside ApartmentFragment, i want to show other things plus the map. Which is the best way to do this? Is possible to put a mapfragment inside a fragment? or I need to transform ApartmentFragment in activity?

推荐答案

大部分什么ü读它是正确的。你只需要使用图形页面而不是 MapFragment

most of what u read it's correct. You'll just use MapView instead of MapFragment

https://developer.android.com/reference/com/google/android/gms/maps/MapView.html

然后你ApartmenFragment你必须让所有的回调,以图形页面

then on your ApartmenFragment you have to make all callbacks to mapview

  • 的onCreate(捆绑)
  • 在onResume()
  • 的onPause()
  • 的onDestroy()
  • 的onSaveInstanceState()
  • onLowMemory()

是这样的:

    private MapView mapView;

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

       // inflate your view `root` and then call ...
       mapView = (MapView) root.indViewById(R.id.map_view);
       mapView.onCreate(savedInstanceState);
       mapView.getMapAsync(this);

       return root;
    }

    @Override public void onResume() {
       super.onResume();
       mapView.onResume();
    }

    @Override public void onPause() {
       super.onPause();
       mapView.onPause();
    }

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

    @Override public void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);
       mapView.onSaveInstanceState(outState);
    }

    @Override public void onLowMemory() {
       super.onLowMemory();
       mapView.onLowMemory();
    }

    @Override public void onMapReady (GoogleMap googleMap) {
          // from https://developer.android.com/reference/com/google/android/gms/maps/OnMapReadyCallback.html

          // and here you can do your map stuff with `googleMap`
    }

这篇关于把地图(谷歌地图V2)的片段里​​最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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