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.
Try this it works for me How to put Google Maps V2 on a Fragment Using ViewPager
Try this
and in your xml
Revised code:
Your xml file-
Your java file (only relevant code)-
Try this i have made this as a sample according to your need
getMap() method is depricated
getMap()
method is depreciated but after theplay-services 9.2
it is removed, so better use getMapAsync(). You can still use getMap() only if you are not willing to update theplay-services 9.2
for your app.To use
getMapAsync()
, implement theOnMapReadyCallback
interface on your activity or fragment:For fragment
For Activity
Fragment in xml
You can use this to making map but it will return map object is null because you don't find solution with getMapAsync to create a new object mMap. It only background task with return null