MapFragment in Fragment, alternatives?

后端 未结 8 607
猫巷女王i
猫巷女王i 2020-12-08 10:32

I need your help... I work on it until 3 days. My app is working with fragments. One of these fragments has to display a map from the Google Maps V2 api for Android.

相关标签:
8条回答
  • 2020-12-08 10:50

    After lot of mistakes I finally made it , here is my MapView Fragment Class :-

    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Toast;
    
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import com.serveroverload.yago.R;
    
    /**
     * @author 663918
     *
     */
    public class HomeFragment extends Fragment implements LocationListener {
        // Class to do operations on the Map
        GoogleMap googleMap;
        private LocationManager locationManager;
    
        public static Fragment newInstance() {
            return new HomeFragment();
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.home_fragment, container, false);
            Bundle bdl = getArguments();
    
            // setuping locatiomanager to perfrom location related operations
            locationManager = (LocationManager) getActivity().getSystemService(
                    Context.LOCATION_SERVICE);
    
            // Requesting locationmanager for location updates
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 1, 1, this);
    
            // To get map from MapFragment from layout
            googleMap = ((MapFragment) getActivity().getFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
    
            // To change the map type to Satellite
            // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    
            // To show our current location in the map with dot
            // googleMap.setMyLocationEnabled(true);
    
            // To listen action whenever we click on the map
            googleMap.setOnMapClickListener(new OnMapClickListener() {
    
                @Override
                public void onMapClick(LatLng latLng) {
                    /*
                     * LatLng:Class will give us selected position lattigude and
                     * longitude values
                     */
                    Toast.makeText(getActivity(), latLng.toString(),
                            Toast.LENGTH_LONG).show();
                }
            });
    
            changeMapMode(3);
    
            // googleMap.setSatellite(true);
            googleMap.setTrafficEnabled(true);
            googleMap.setBuildingsEnabled(true);
            googleMap.setMyLocationEnabled(true);
    
            return v;
        }
    
        private void doZoom() {
            if (googleMap != null) {
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        new LatLng(18.520430, 73.856744), 17));
            }
        }
    
        private void changeMapMode(int mapMode) {
    
            if (googleMap != null) {
                switch (mapMode) {
                case 0:
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
                    break;
    
                case 1:
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    break;
    
                case 2:
                    googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                    break;
    
                case 3:
                    googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                    break;
    
                case 4:
                    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                    break;
    
                default:
                    break;
                }
            }
        }
    
        private void createMarker(double latitude, double longitude) {
            // double latitude = 17.385044;
            // double longitude = 78.486671;
    
            // lets place some 10 random markers
            for (int i = 0; i < 10; i++) {
                // random latitude and logitude
                double[] randomLocation = createRandLocation(latitude, longitude);
    
                // Adding a marker
                MarkerOptions marker = new MarkerOptions().position(
                        new LatLng(randomLocation[0], randomLocation[1])).title(
                        "Hello Maps " + i);
    
                Log.e("Random", "> " + randomLocation[0] + ", " + randomLocation[1]);
    
                // changing marker color
                if (i == 0)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
                if (i == 1)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                if (i == 2)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
                if (i == 3)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                if (i == 4)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                if (i == 5)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
                if (i == 6)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED));
                if (i == 7)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
                if (i == 8)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
                if (i == 9)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
    
                googleMap.addMarker(marker);
    
                // Move the camera to last position with a zoom level
                if (i == 9) {
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(randomLocation[0], randomLocation[1]))
                            .zoom(15).build();
    
                    googleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                }
            }
    
        }
    
        /*
         * creating random postion around a location for testing purpose only
         */
        private double[] createRandLocation(double latitude, double longitude) {
    
            return new double[] { latitude + ((Math.random() - 0.5) / 500),
                    longitude + ((Math.random() - 0.5) / 500),
                    150 + ((Math.random() - 0.5) * 10) };
        }
    
        @Override
        public void onLocationChanged(Location location) {
    
            if (null != googleMap) {
                // To get lattitude value from location object
                double latti = location.getLatitude();
                // To get longitude value from location object
                double longi = location.getLongitude();
    
                // To hold lattitude and longitude values
                LatLng position = new LatLng(latti, longi);
    
                createMarker(latti, longi);
    
                // Creating object to pass our current location to the map
                MarkerOptions markerOptions = new MarkerOptions();
                // To store current location in the markeroptions object
                markerOptions.position(position);
    
                // Zooming to our current location with zoom level 17.0f
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,
                        17f));
    
                // adding markeroptions class object to the map to show our current
                // location in the map with help of default marker
                googleMap.addMarker(markerOptions);
            }
    
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onDestroyView() {
            // TODO Auto-generated method stub
            super.onDestroyView();
    
            locationManager.removeUpdates(this);
    
            android.app.Fragment fragment = getActivity().getFragmentManager()
                    .findFragmentById(R.id.map);
            if (null != fragment) {
                android.app.FragmentTransaction ft = getActivity()
                        .getFragmentManager().beginTransaction();
                ft.remove(fragment);
                ft.commit();
            }
        }
    
    
    }
    

    My Xml file looks like this:-

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

    And result looks like this :-

    Most Important thing to note is that DO Not Mix app.Fragment with v4.Fragments else app will crash badly.

    As you can see I have used app.Fragment to attach and remove my MapView Fragment

    Hope it will help Somebody

    0 讨论(0)
  • 2020-12-08 10:59
                <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:id="@+id/location_map"
                android:layout_above="@id/atmLocation_recyclerView"
                />
    
    
    View root= inflater.inflate(R.layout.fragment_a_t_m_locations, container, false);
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.location_map);
        mapFragment.getMapAsync(googleMap -> {
            mMap=googleMap;
            if(mMap!=null){
                mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            }
        });
    
    0 讨论(0)
  • 2020-12-08 11:02

    You can try adding the MapFragment inside of a FragmentContainerView from androidx Fragment package:

    • FragmentContainerView
    • From: implementation "androidx.fragment:fragment:$1.2.4"
    • Check: https://developer.android.com/jetpack/androidx/releases/fragment
    0 讨论(0)
  • 2020-12-08 11:05

    In Your Class

          SupportMapFragment mSupportMapFragment;
          private GoogleMap googleMap;
          int ZOOM_LEVEL=15;
    
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
           View mTrackView = inflater
                .inflate(R.layout.mylayout, container, false);
            mSupportMapFragment = SupportMapFragment.newInstance();
            FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.mapwhere, mSupportMapFragment);
            fragmentTransaction.commit();
    
            return mTrackView;
        }
    
          @Override
          public void onStart() {
            // TODO Auto-generated method stub
              super.onStart();
    
            if(mSupportMapFragment!=null){
    
                googleMap = mSupportMapFragment.getMap();
                if(googleMap!=null){
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                googleMap.getUiSettings().setMyLocationButtonEnabled(false);
                googleMap.setMyLocationEnabled(false);
    
    
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                        new LatLng(12.12122,
                            17.22323), ZOOM_LEVEL);
                googleMap.animateCamera(cameraUpdate);
                  }
                }
          }
    

    mylayout.xml

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical" >
         <FrameLayout
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1.03"
    
           android:id="@+id/mapwhere" />
    
    
          <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
    
        </LinearLayout>
    
    0 讨论(0)
  • 2020-12-08 11:06

    As described here

    To display MapFragment inside the fragment(NestedFragment): At this point I believe you have

    1. added necessary permission on manifest
    2. added google play service as a lib project
    3. api key in manifest file. 4.

    where.xml

     <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:map="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical" >
             <FrameLayout
               android:layout_width="match_parent"
               android:layout_height="0dp"
               android:layout_weight="1.03"
               android:name="com.google.android.gms.maps.SupportMapFragment"
               android:id="@+id/mapwhere" />
    
    
              <TextView
               android:layout_width="match_parent"
               android:layout_height="wrap_content"/>
    
            </LinearLayout>
    

    class:

     public class WhereFragment extends SupportMapFragment {
    
        @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
         {
          super.onCreateView(inflater, container, savedInstanceState);
          View root = inflater.inflate(R.layout.where, null, false); 
          initilizeMap();
          return root;
         }
    
        private void initilizeMap()
         {
          mSupportMapFragment = (SupportMapFragment) getFragmentManager().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)
          {
           googleMap = mSupportMapFragment.getMap();
           if (googleMap != null)
            googleMap.setOnMapClickListener(new OnMapClickListener()
            {
             @Override
             public void onMapClick(LatLng point)
             {
              //TODO: your onclick stuffs
             }
            });
          }
         }
        }
    

    Documentation:

    Nested Fragments You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.

    To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:

    Fragment videoFragment = new VideoPlayerFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.video_fragment, videoFragment).commit(); From within a nested fragment, you can get a reference to the parent fragment by calling getParentFragment().

    The Android Support Library also now supports nested fragments, so you can implement nested fragment designs on Android 1.6 and higher.

    Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.

    source:http://developer.android.com/about/versions/android-4.2.html#NestedFragments

    This will also fix for:

     11-06 11:36:01.509: E/AndroidRuntime(6309): FATAL EXCEPTION: main
        11-06 11:36:01.509: E/AndroidRuntime(6309): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at com.abc.android.ui.WhereFragment.onCreateView(WhereFragment.java:60)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        11-06 11:36:01.509: E/AndroidRuntime(6309):  at a ...
    
    0 讨论(0)
  • 2020-12-08 11:08

    In your layout file

    <fragment
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
    

    In your fragment onCreate(), reference the map fragment in your fragment layout file using childFragmentManager

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            val mapFragment: SupportMapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
            // Set callback on the fragment
            mapFragment.getMapAsync(this)
    
    0 讨论(0)
提交回复
热议问题