GetMapAsync () in fragment

I am having problems implementing a Google map in fragment.

This is my part of the class of my fragment:

public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback { // Google Map private GoogleMap googleMap; private int i; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById( R.id.map)).getMapAsync(this); 

I get an error in getMapAsync (this). I say incompatible type.

He says:

required: com.google.android.gms.map.GoogleMap

found: void

By the way, here is my xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment class="com.google.android.gms.maps.SupportMapFragment" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> 
+14
android android-studio android-fragments google-maps supportmapfragment
source share
4 answers

In your xml layout you have to do this

 <FrameLayout 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"> <com.google.android.gms.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout> 

inside your fragment implement this

 private MapView mapView; private GoogleMap googleMap; 

override onCreateView if onCreateView does not exist, as in the code below

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.your_layout, container, false); } 

override onViewCreated () inside this onViewCreated () insert this

 mapView = (MapView) view.findViewById(R.id.map); mapView.onCreate(savedInstanceState); mapView.onResume(); mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment 

when you have already implemented OnMapReadyCallback in your snippet, add this code / like this

 @Override public void onMapReady(GoogleMap map) { googleMap = map; } 

hope this helps you.

+51
source share

After a few overheating in my head, thanks to this post and fooobar.com/questions/834215 / ... , I would say that there are currently two ways to add MapFragment to your application:

 <fragment xmlns:android="http://schemas.android.com/apk/res/android" class="com.google.android.gms.maps.SupportMapFragment" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> 

So you should use the standard snippet and implement this internally. In fact, you are inserting a fragment into another:

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); supportMapFragment.getMapAsync(this); } 

Another way is to use a real SupportMapFragment and implement it as follows:

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); getMapAsync(this); } 

And they work without having to set the layout in onCreateView , also without calling mapView.onCreate(savedInstanceState); and mapView.onResume(); manually inside onViewCreated() , which is not recommended in any case.

Hope this helps!

+1
source share
  SupportMapFragment mMapFragment = SupportMapFragment.newInstance(); FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.flMap, mMapFragment); fragmentTransaction.commit(); mMapFragment.getMapAsync(this); 
+1
source share

Your code returns this error because getMapAsync() does not return anything. If you look at the documentation for this function:

public void getMapAsync (OnMapReadyCallback callback)

Sets the callback object that will fire when the GoogleMap instance is ready to use.

Note that:

This method must be called from the main thread. The callback will be executed in the main thread. In the case when the Google Play service is not installed on the user device, the callback will not be launched until the user installs it. In the rare case when GoogleMap is destroyed immediately after creation, the callback does not start. The GoogleMap object provided by the callback does not matter.

OnMapReadyCallback is an interface that must be implemented and passed through this function. Nothing is currently bound to your googleMap variable, instead you should set its value in this code block, which implements OnMapReadyCallback

 @Override public void onMapReady(GoogleMap googleMap) { this.googleMap = googleMap; } 

This code should be in your fragment somewhere. Since your fragment already implements this interface, you pass it as this .

0
source share

All Articles