How to put Google Maps V2 on a Fragment using ViewPager

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

    For the issue of getting a NullPointerException when we change the Tabs in a FragmentTabHost you just need to add this code to your class which has the TabHost. I mean the class where you initialize the tabs. This is the code :

    /**** Fix for error : Activity has been destroyed, when using Nested tabs 
     * We are actually detaching this tab fragment from the `ChildFragmentManager`
     * so that when this inner tab is viewed back then the fragment is attached again****/
    
    import java.lang.reflect.Field;
    
    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    
    0 讨论(0)
  • 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:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.ahmedsamra.mansouratourguideapp.MapsActivity" />
    

    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;
        }
    
    }
    
    0 讨论(0)
  • 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:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <com.google.android.gms.maps.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </RelativeLayout>
    

    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.

    0 讨论(0)
  • 2020-11-22 05:39
    public class DemoFragment extends Fragment {
    
    
    MapView mapView;
    GoogleMap map;
    LatLng CENTER = null;
    
    public LocationManager locationManager;
    
    double longitudeDouble;
    double latitudeDouble;
    
    String snippet;
    String title;
    Location location;
    String myAddress;
    
    String LocationId;
    String CityName;
    String imageURL;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater
                    .inflate(R.layout.fragment_layout, container, false);
    
        mapView = (MapView) view.findViewById(R.id.mapView);
            mapView.onCreate(savedInstanceState);
    
      setMapView();
    
    
     }
    
     private void setMapView() {
        try {
            MapsInitializer.initialize(getActivity());
    
            switch (GooglePlayServicesUtil
                    .isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                // Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT)
                // .show();
    
                // Gets to GoogleMap from the MapView and does initialization
                // stuff
                if (mapView != null) {
    
                    locationManager = ((LocationManager) getActivity()
                            .getSystemService(Context.LOCATION_SERVICE));
    
                    Boolean localBoolean = Boolean.valueOf(locationManager
                            .isProviderEnabled("network"));
    
                    if (localBoolean.booleanValue()) {
    
                        CENTER = new LatLng(latitude, longitude);
    
                    } else {
    
                    }
                    map = mapView.getMap();
                    if (map == null) {
    
                        Log.d("", "Map Fragment Not Found or no Map in it!!");
    
                    }
    
                    map.clear();
                    try {
                        map.addMarker(new MarkerOptions().position(CENTER)
                                .title(CityName).snippet(""));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    map.setIndoorEnabled(true);
                    map.setMyLocationEnabled(true);
                    map.moveCamera(CameraUpdateFactory.zoomTo(5));
                    if (CENTER != null) {
                        map.animateCamera(
                                CameraUpdateFactory.newLatLng(CENTER), 1750,
                                null);
                    }
                    // add circle
                    CircleOptions circle = new CircleOptions();
                    circle.center(CENTER).fillColor(Color.BLUE).radius(10);
                    map.addCircle(circle);
                    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
                }
                break;
            case ConnectionResult.SERVICE_MISSING:
    
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
    
                break;
            default:
    
            }
        } catch (Exception e) {
    
        }
    
    }
    

    in fragment_layout

    <com.google.android.gms.maps.MapView
                    android:id="@+id/mapView"
                    android:layout_width="match_parent"
                    android:layout_height="160dp"                    
                    android:layout_marginRight="10dp" />
    
    0 讨论(0)
  • 2020-11-22 05:41

    According to https://developer.android.com/about/versions/android-4.2.html#NestedFragments, you can use nested fragments to achieve this by calling getChildFragmentManager() if you still want to use the Google Maps fragment instead of the view inside your own fragment:

    SupportMapFragment mapFragment = new SupportMapFragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.content, mapFragment).commit();
    

    where "content" is the root layout in your fragment (preferably a FrameLayout). The advantage of using a map fragment is that then the map lifecycle is managed automatically by the system.

    Although the documentation says "You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically. ", I have somehow successfully done this and it worked fine. Here is my code:
    In the fragment's onCreateView() method:

    View view = inflater.inflate(R.layout.layout_maps, container, false);
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(...);
    

    In the layout:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    Hope it helps!

    0 讨论(0)
  • 2020-11-22 05:48

    Dynamically adding map fragment to view Pager:

    If you are targeting an application earlier than API level 12 make an instance of SupportedMapFragment and add it to your view page adapter.

    SupportMapFragment supportMapFragment=SupportMapFragment.newInstance();
            supportMapFragment.getMapAsync(this);
    

    API level 12 or higher support MapFragment objects

    MapFragment mMapFragment=MapFragment.newInstance();
                mMapFragment.getMapAsync(this);
    
    0 讨论(0)
提交回复
热议问题