0

I am developing an app that use fragments. One of my fragments i want to use google map but i am getting errors.

    public class Map extends Fragment {
        public Map() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment


            GoogleMap map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//Error:(38, 84) error: inconvertible types
//required: SupportMapFragment
//found:    Fragment
return inflater.inflate(R.layout.fragment_map, container, false);

        }
    }

When i trying to cast map fragment i am getting the error in comment. What should i do to solve this? Thanks for everyone. Edit: the layout ;

<fragment
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment" />
mesopotamia
  • 393
  • 1
  • 5
  • 19

3 Answers3

0

You can go through this approach.
This is my Fragment class which has MapView

public class DashBoardFragment extends Fragment{
    //-------------- class variables
    private MapView mMapView;
    private GoogleMap mGoogleMap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_dash_board, container, false);
      mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mGoogleMap = mMapView.getMap();     
}

}


Below is my xml for the fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.dev.abc">

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_dashBoard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>


Manifest should have the following along with permissions

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your api key" />
Sreehari
  • 5,621
  • 2
  • 25
  • 59
0

You are casting to a mapFragment AFTER the return statement. Since the program believes the method is complete at that point it never reaches it. Try this:

Inflater v = inflater.inflate(R.layout.fragment_map, container, false);
GoogleMap map = ((SupportMapFragment)v.getFragmentManager().findFragmentById(R.id.map)).getMap();
return v;

Also I don't see you using an intent to start the google map activity.

0

Latest update of Google for Maps, only MapView is supported for fragments.

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Dhina k
  • 1,481
  • 18
  • 24