How to put Google Maps V2 on a Fragment using ViewPager

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

    i just create MapActivity and inflate it to fragment . MapActivity.java:

    package com.example.ahmedsamra.mansouratourguideapp;
    
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    
    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    
        private GoogleMap mMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_categories);//layout for container
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, new MapFragment())
                    .commit();
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }
    
    
        /**
         * Manipulates the map once available.
         * This callback is triggered when the map is ready to be used.
         * This is where we can add markers or lines, add listeners or move the camera. In this case,
         * we just add a marker near Sydney, Australia.
         * If Google Play services is not installed on the device, the user will be prompted to install
         * it inside the SupportMapFragment. This method will only be triggered once the user has
         * installed Google Play services and returned to the app.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
    
            // Add a marker in Sydney and move the camera
            LatLng mansoura = new LatLng(31.037933, 31.381523);
            mMap.addMarker(new MarkerOptions().position(mansoura).title("Marker in mansoura"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(mansoura));
        }
    }
    

    activity_map.xml:

    
    

    MapFragment.java:-

    package com.example.ahmedsamra.mansouratourguideapp;
    
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    /**
     * A simple {@link Fragment} subclass.
     */
    public class MapFragment extends Fragment {
    
    
        public MapFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_maps,container,false);
            return rootView;
        }
    
    }
    

提交回复
热议问题