How to put Google Maps V2 on a Fragment using ViewPager

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

    By using this code we can setup MapView anywhere, inside any ViewPager or Fragment or Activity.

    In the latest update of Google for Maps, only MapView is supported for fragments. MapFragment & SupportMapFragment doesn't work. I might be wrong but this is what I saw after trying to implement MapFragment & SupportMapFragment.

    Setting up the layout for showing the map in the file location_fragment.xml:

    
    
    
        
    
    
    

    Now, we code the Java class for showing the map in the file MapViewFragment.java:

    public class MapViewFragment extends Fragment {
    
        MapView mMapView;
        private GoogleMap googleMap;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.location_fragment, container, false);
    
            mMapView = (MapView) rootView.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);
    
            mMapView.onResume(); // needed to get the map to display immediately
    
            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            mMapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap mMap) {
                    googleMap = mMap;
    
                    // For showing a move to my location button
                    googleMap.setMyLocationEnabled(true);
    
                    // For dropping a marker at a point on the Map
                    LatLng sydney = new LatLng(-34, 151);
                    googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
    
                    // For zooming automatically to the location of the marker
                    CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                }
            });
    
            return rootView;
        }
    
        @Override
        public void onResume() {
            super.onResume();
            mMapView.onResume();
        }
    
        @Override
        public void onPause() {
            super.onPause();
            mMapView.onPause();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            mMapView.onDestroy();
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mMapView.onLowMemory();
        }
    }
    

    Finally you need to get the API Key for your app by registering your app at Google Cloud Console. Register your app as Native Android App.

提交回复
热议问题