0

I would like to put an Google Map in a fragment, but please be noted that I want to put it inside the onActivityCreated, and the fragment class just need to extend fragment class only.

And my final layout will be: This is a fragment contains a text, map and button.

~~~~~~~~~~~~~~~

Textview.....

Map.....

Button....

~~~~~~~~~~~~~~~

Also, the map need include my current location.

May I know how to do that? with java and xml please :)

Tianbing Leng
  • 560
  • 1
  • 8
  • 22
  • Look here (http://stackoverflow.com/questions/16536414/how-to-use-mapview-in-android-using-google-map-v2) , maybe will be useful – Luca Sepe Mar 10 '14 at 15:22
  • Need more resource... I want to added into the onActivityCreated() method. – Tianbing Leng Mar 11 '14 at 13:12
  • Keep in mind you might happen to stumble upon state loss exception. Check [this](http://stackoverflow.com/questions/16265733/failure-delivering-result-onactivityforresult) link how you should approach inflating fragment from `onActivityResult`. – Yordan Lyubenov Mar 11 '14 at 14:04

1 Answers1

0

In your Fragment layout put a MapView widget:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <!-- My TextView -->   

    <com.google.android.gms.maps.MapView android:id="@+id/my_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        map:uiZoomControls="false"
        map:uiCompass="false"
        map:uiRotateGestures="false"
        map:uiScrollGestures="true"
        map:uiTiltGestures="false"
        map:uiZoomGestures="true"
        android:visibility="visible"/>

   <!-- My Button and eventually others widgets -->

 </RelativeLayout>

Your fragment will inflate your custom layout with the MapView and the others widgets you need in . Here a pseudo code about how to setup the MapView.

public class MyCustomFragmentWithMap extends Fragment {

   private MapView   mMapView = null;
   private GoogleMap mMyMap   = null;

   @Override 
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      try {
        MapsInitializer.initialize(getActivity());
      } catch (GooglePlayServicesNotAvailableException err) {
        Log.e(TAG, err.getMessage(), err);
      }

      mMapView = (MapView)getView().findViewById(R.id.my_map);
      mMapView.onCreate(savedInstanceState);
      mMyMap = mMapView.getMap();
      mMyMap.setMyLocationEnabled(true);
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.my_custom_fragment_with_map, container, false);

      /* All the widgets setup as usual ... */

      return view;
   }

   @Override
   public void onResume() {
      super.onResume();
      if (mMapView != null)
        mMapView.onResume();
   }

   @Override
   public void onPause() {
     if (mMapView != null)
        mMapView.onPause();
     super.onPause();
   }

   @Override
   public void onDestroy() {
     if (mMapView != null)
       mMapView.onDestroy();
     super.onDestroy();
   }

   @Override
   public void onLowMemory() {
     super.onLowMemory();
     if (mMapView != null)
       mMapView.onLowMemory();
   }

   @Override
   public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      if (mMapView != null)
         mMapView.onSaveInstanceState(outState);
   }

I'm assuming that you already know how to setup the Google Maps Android API v2 (obtaining the api key, setting permissions in manifest and all the boring stuffs :-P).

Luca Sepe
  • 2,435
  • 1
  • 20
  • 26