How to put Google Maps V2 on a Fragment using ViewPager

前端 未结 13 2139
攒了一身酷
攒了一身酷 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:36

    here what i did in detail:

    From here you can get google map api key

    alternative and simple way

    first log in to your google account and visit google libraries and select Google Maps Android API

    dependency found in android studio default map activity :

    compile 'com.google.android.gms:play-services:10.0.1'
    

    put your key into android mainifest file under application like below

    in AndroidMainifest.xml make these changes:

        // required permission
        
    
    
            // google map api key put under/inside 
            // android:value="YOUR API KEY"
            
    

    Fragment code :

    public class MainBranchFragment extends Fragment implements OnMapReadyCallback{
    
    private GoogleMap mMap;
        public MainBranchFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view= inflater.inflate(R.layout.fragment_main_branch, container, false);
            SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.main_branch_map);
            mapFragment.getMapAsync(this);
            return view;
        }
    
    
    
    
         @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
                LatLng UCA = new LatLng(-34, 151);
                mMap.addMarker(new MarkerOptions().position(UCA).title("YOUR TITLE")).showInfoWindow();
    
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(UCA,17));
    
            }
        }
    

    in you fragment xml :

    
    

提交回复
热议问题