1

I have a fragment (ApartmentFragment) which is a page that show details of an apartment. I want to add a little map to show where the apartment is. just for clarity, I'm using the MaterialNavigationDrawer that is on github, my app is composed by a single Activity (the navigation drawer) and many fragments.

Every stackoverflow post I have read didn't helped me. Many of them were a bit confusing. I don't want only the map inside ApartmentFragment, i want to show other things plus the map. Which is the best way to do this? Is possible to put a mapfragment inside a fragment? or I need to transform ApartmentFragment in activity?

retrobitguy
  • 535
  • 1
  • 6
  • 18
  • What have you tried so far? Post your code. You can have a layout which contains `com.google.android.gms.maps.MapView`, from which you can get a reference to `com.google.android.gms.maps.GoogleMap` and manipulate it. Also checkout Airbnb's AirMapView https://github.com/airbnb/AirMapView – hidro May 21 '15 at 10:04
  • I just followed the instructions in this [post](https://stackoverflow.com/questions/26174527/android-mapview-in-fragment). Before this question I was not really aware of existence of MapView. Thank you for suggesting me AirMapView, it looks a very good library, i will checkout :) – retrobitguy May 23 '15 at 12:40

1 Answers1

3

most of what u read it's correct. You'll just use MapView instead of MapFragment

https://developer.android.com/reference/com/google/android/gms/maps/MapView.html

then on your ApartmenFragment you have to make all callbacks to mapview

  • onCreate(Bundle)
  • onResume()
  • onPause()
  • onDestroy()
  • onSaveInstanceState()
  • onLowMemory()

like this:

    private MapView mapView;

    @Nullable @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

       // inflate your view `root` and then call ...
       mapView = (MapView) root.indViewById(R.id.map_view);
       mapView.onCreate(savedInstanceState);
       mapView.getMapAsync(this);

       return root;
    }

    @Override public void onResume() {
       super.onResume();
       mapView.onResume();
    }

    @Override public void onPause() {
       super.onPause();
       mapView.onPause();
    }

    @Override public void onDestroyView() {
       super.onDestroyView();
       mapView.onDestroy();
    }

    @Override public void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);
       mapView.onSaveInstanceState(outState);
    }

    @Override public void onLowMemory() {
       super.onLowMemory();
       mapView.onLowMemory();
    }

    @Override public void onMapReady (GoogleMap googleMap) {
          // from https://developer.android.com/reference/com/google/android/gms/maps/OnMapReadyCallback.html

          // and here you can do your map stuff with `googleMap`
    }
Budius
  • 39,391
  • 16
  • 102
  • 144