How to put Google Maps V2 on a Fragment using ViewPager

前端 未结 13 1969
攒了一身酷
攒了一身酷 2020-11-22 04:56

I am trying to do a tab layout same in Play Store. I got to display the tab layout using a fragments and viewpager from androidhive. However, I can\'t implement google maps

13条回答
  •  清酒与你
    2020-11-22 05:30

    Latest stuff with getMapAsync instead of the deprecated one.

    1. check manifest for

    
    

    You can get the API Key for your app by registering your app at Google Cloud Console. Register your app as Native Android App

    2. in your fragment layout .xml add FrameLayout(not fragment):

                      
    

    or whatever height you want

    3. In onCreateView in your fragment

        private SupportMapFragment mSupportMapFragment; 
    
        mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapwhere);
        if (mSupportMapFragment == null) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mSupportMapFragment = SupportMapFragment.newInstance();
            fragmentTransaction.replace(R.id.mapwhere, mSupportMapFragment).commit();
        }
    
        if (mSupportMapFragment != null)
        {
            mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override public void onMapReady(GoogleMap googleMap) {
                    if (googleMap != null) {
    
                        googleMap.getUiSettings().setAllGesturesEnabled(true);
    
                          -> marker_latlng // MAKE THIS WHATEVER YOU WANT
    
                            CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(15.0f).build();
                            CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
                            googleMap.moveCamera(cameraUpdate);
    
                    }
    
                }
            });
    

提交回复
热议问题