AI-generated Key Takeaways
-
MapFragmentprovides a simple way to embed a Google Map within your Android application by wrapping a map view and handling its lifecycle. -
You need to acquire a
GoogleMapinstance usinggetMapAsync()to interact with the map. -
The view is automatically initialized and can be removed when
onDestroyView()is called, requiring recreation viaonCreateView(). -
Remember to release
GoogleMapobjects when the view's lifecycle ends to avoid memory leaks. -
For API levels below 12, consider using
SupportMapFragmentfrom the Android support library.
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. Being a fragment, this component can be added to an activity's layout file simply with the XML below.
<fragment
class="com.google.android.libraries.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>GoogleMap must be acquired using getMapAsync(OnMapReadyCallback). This class
automatically initializes the maps system and the view.
A view can be removed when the MapFragment's onDestroyView() method is called and the
useViewLifecycleInFragment(boolean) option is set. When this happens the
MapFragment is no longer valid until the view is recreated again later when the MapFragment's
onCreateView(LayoutInflater, ViewGroup, Bundle) method is called.
Any objects obtained from the GoogleMap are associated with the view. It's important
to not hold on to objects (e.g. Marker) beyond
the view's life. Otherwise it will cause a memory leak as the view cannot be released.
Use this class only if you are targeting API 12 and above. Otherwise, use SupportMapFragment by adding the Android support library to your build path.
Developer Guide
For more information, read the Google Maps SDK for Android developer guide.
Inherited Constant Summary
Public Constructor Summary
|
MapFragment()
Creates a map fragment.
|
Public Method Summary
| void |
getMapAsync(OnMapReadyCallback callback)
Sets a callback object which will be triggered when the
GoogleMap instance is ready to
be used. |
| static MapFragment | |
| static MapFragment |
newInstance()
Creates a map fragment, using default options.
|
| void |
onActivityCreated(Bundle savedInstanceState)
|
| void |
onAttach(Activity activity)
|
| void |
onConfigurationChanged(Configuration config)
|
| void |
onCreate(Bundle savedInstanceState)
|
| View |
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
|
| void | |
| void | |
| final void |
onEnterAmbient(Bundle ambientDetails)
You must call this method from the parent WearableActivity's corresponding method.
|
| final void |
onExitAmbient()
You must call this method from the parent WearableActivity's corresponding method.
|
| void |
onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState)
Parse attributes during inflation from a view hierarchy into the arguments we handle.
|
| void | |
| void |
onPause()
|
| void |
onResume()
|
| void |
onSaveInstanceState(Bundle outState)
Provides a
Bundle to store the state of the Fragment before it gets destroyed. |
| void |
onStart()
|
| void |
onStop()
|
| void |
setArguments(Bundle args)
|
Inherited Method Summary
Public Constructors
public MapFragment ()
Creates a map fragment. This constructor is public only for use by an inflater. Use newInstance() to create a MapFragment programmatically.
Public Methods
public void getMapAsync (OnMapReadyCallback callback)
Sets a callback object which will be triggered when the GoogleMap instance is ready to
be used.
Note that:
- This method must be called from the main thread.
- The callback will be executed in the main thread.
- In the case where Google Play services is not installed on the user's device, the callback will not be triggered until the user installs it.
- In the rare case where the GoogleMap is destroyed immediately after creation, the callback is not triggered.
- The
GoogleMapobject provided by the callback is non-null.
Parameters
| callback | The callback object that will be triggered when the map is ready to be used. |
|---|
public static MapFragment newInstance (GoogleMapOptions options)
Creates a map fragment with the given options.
Parameters
| options |
|---|
public void onActivityCreated (Bundle savedInstanceState)
Parameters
| savedInstanceState |
|---|
public void onAttach (Activity activity)
Parameters
| activity |
|---|
public void onConfigurationChanged (Configuration config)
Parameters
| config |
|---|
public void onCreate (Bundle savedInstanceState)
Parameters
| savedInstanceState |
|---|
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Parameters
| inflater | |
|---|---|
| container | |
| savedInstanceState |
public void onDestroy ()
public void onDestroyView ()
public final void onEnterAmbient (Bundle ambientDetails)
You must call this method from the parent WearableActivity's corresponding method.
Parameters
| ambientDetails |
|---|
public final void onExitAmbient ()
You must call this method from the parent WearableActivity's corresponding method.
public void onInflate (Activity activity, AttributeSet attrs, Bundle savedInstanceState)
Parse attributes during inflation from a view hierarchy into the arguments we handle.
Parameters
| activity | |
|---|---|
| attrs | |
| savedInstanceState |
public void onLowMemory ()
public void onPause ()
public void onResume ()
public void onSaveInstanceState (Bundle outState)
Provides a Bundle to store the state of the Fragment before it gets destroyed. It can
later be retrieved when onCreate(Bundle) is called again.
Parameters
| outState |
|---|
public void onStart ()
public void onStop ()
public void setArguments (Bundle args)
Parameters
| args |
|---|