Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Google Maps object inside a Fragment

I am using fragments in my application and I am new for this. There is a fragment in which I want to display Google Map and want to get its object, for this I have a fragment in my XML and I want to inflate it in the Fragment itself. When I am trying to inflate the map view its showing me error for getSupportFragmentManager().

How do I get the map object then that is the main issue. My XML is like this :-

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

And my Fragment in which I want to get the Google Map object is like that :-

public class FindMyCar extends Fragment {

    private TextView mTvFind;
    private TextView mTvPark;
    private EditText mEtParkHint;

    private GoogleMap map;

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


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

        View view = inflater.inflate(R.layout.findmycar, null);

        initViews(view);

        Typeface type = Typeface.createFromAsset(getActivity().getResources().getAssets(),"Multicolore.otf"); 
        mTvFind.setTypeface(type);
        mTvPark.setTypeface(type);
        mEtParkHint.setTypeface(type);

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


        return view;
    }


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


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


    private void initViews(View view){


        mTvPark = (TextView) view.findViewById(R.id.tvFindCarPark);
        mTvFind = (TextView) view.findViewById(R.id.tvFindCar);
        mEtParkHint = (EditText) view.findViewById(R.id.etParkHint);
    }

Can someone help me to get the object for my map so that I can show current location and draw the marker there.

Any help would be appreciable. Thanks in advance.

like image 797
Salman Khan Avatar asked Feb 26 '14 05:02

Salman Khan


People also ask

What is MapFragment?

public class MapFragment extends Fragment. A Map component in an app. This fragment is the simplest way to place a map in an application. It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.

Can a fragment contain an activity?

A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input events, and you can add or remove fragments while the containing activity is running. This document describes how to create a fragment and include it in an activity.


1 Answers

getMap() method is deprecated

getMap() method is deprecated but after the play-services 9.2it is removed, so better use getMapAsync(). You can still use getMap() only if you are not willing to update the play-services 9.2 for your app.

To use getMapAsync(), implement the OnMapReadyCallback interface on your activity or fragment:

For fragment:

public class MapFragment extends android.support.v4.app.Fragment
      implements OnMapReadyCallback { /* ... */ }

Then, while initializing the map, use getMapAsync() instead of getMap():

//call this method in your onCreateMethod
 private void initializeMap() {
  if (mMap == null) {
    SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
    mapFrag.getMapAsync(this);
  }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();// do your map stuff here
}

For Activity:

public class MapActivity extends FragmentActivity
      implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():


//call this method in your onCreateMethod
 private void initializeMap() {
  if (mMap == null) {
    SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
    mapFrag.getMapAsync(this);
  }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();// do your map stuff here
}

Fragment in XML:

   <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"/>
like image 136
Dharmbir Singh Avatar answered Oct 24 '22 00:10

Dharmbir Singh