Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is recommended way to use Google Map v2 inside fragment?

I want to add map inside fragment with custom layout.

  1. I can do it using ChildFragmentManager that will add SupportMapFragment. This approach im currently using. However it has disadvantage because child fragment transaction is asynchronous and its hard to guarantee that getMap won't return null.
  2. Another way is to extend SupportMapFragment store mapView from super onCreateView

    mapView = super.onCreateView(inflater, container, savedInstanceState);

    and insert it to inflated layout. Primary problem is that then fragment try to restore from saved state Google Maps SDK crushes internally.

Is there any other way to solve this problem. It would be great if somebody from Google Map team will recommend right approach because you haven't included anything like this to samples.

like image 225
Alexey Zakharov Avatar asked Mar 05 '13 14:03

Alexey Zakharov


People also ask

How do I float a div in Google Maps?

Just need to move the map below this box. Work to me. Show activity on this post. Just set the position of the div and you may have to set the z-index.

What is support map fragment?

public class SupportMapFragment 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.


1 Answers

All FragmentTransactions are asynchronous. If you would like your transaction to happen immediately, you'll have to force them through like so:

getChildFragmentManager().beginTransaction.add(R.id.container, new MyMapFragment(), "MyMapFragment").commit();
getChildFragmentManager().executePendingTransactions();
/* getMap() should not return null here */

From the Android Developer Site:

After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread. If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so. Note that all callbacks and other related behavior will be done from within this call, so be careful about where this is called from.

Returns
Returns true if there were any pending transactions to be executed.

like image 176
Michael Celey Avatar answered Sep 17 '22 18:09

Michael Celey