MapFragment Stay organized with collections Save and categorize content based on your preferences.
Page Summary
MapFragment is a fragment that provides a simple way to embed a map component in an Android application.
To use a MapFragment, it can be added directly to an activity's layout file using XML.
A GoogleMap instance is obtained using
getMapAsync(OnMapReadyCallback), which handles the initialization of the maps system and the view.Objects obtained from GoogleMap should not be held beyond the view's life to avoid memory leaks.
MapFragment is suitable for API 12 and above; for older APIs, SupportMapFragment should be used.
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 android:layout_width="match_parent" android:layout_height="match_parent"/>
GoogleMap must beacquired using getMapAsync(OnMapReadyCallback). This class automatically initializes the maps system and the view.A view can be removed when the MapFragment'sonDestroyView() method is called and the GoogleMapOptions.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 theGoogleMap 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 theGoogle Maps SDK for Android developer guide.
Inherited Constant Summary
| int | TRIM_MEMORY_BACKGROUND | |
| int | TRIM_MEMORY_COMPLETE | |
| int | TRIM_MEMORY_MODERATE | |
| int | TRIM_MEMORY_RUNNING_CRITICAL | |
| int | TRIM_MEMORY_RUNNING_LOW | |
| int | TRIM_MEMORY_RUNNING_MODERATE | |
| int | TRIM_MEMORY_UI_HIDDEN |
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. |
| staticMapFragment | |
| staticMapFragment | newInstance() Creates a map fragment, using default options. |
| void | onActivityCreated(Bundle savedInstanceState) |
| void | |
| void | |
| View | |
| 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
| void | |
| final boolean | equals(Object arg0) |
| finalActivity | getActivity() |
| boolean | getAllowEnterTransitionOverlap() |
| boolean | getAllowReturnTransitionOverlap() |
| finalBundle | getArguments() |
| finalFragmentManager | getChildFragmentManager() |
| Context | getContext() |
| Transition | getEnterTransition() |
| Transition | getExitTransition() |
| finalFragmentManager | getFragmentManager() |
| finalObject | getHost() |
| final int | getId() |
| finalLayoutInflater | getLayoutInflater() |
| LoaderManager | getLoaderManager() |
| finalFragment | getParentFragment() |
| Transition | getReenterTransition() |
| finalResources | getResources() |
| final boolean | getRetainInstance() |
| Transition | getReturnTransition() |
| Transition | getSharedElementEnterTransition() |
| Transition | getSharedElementReturnTransition() |
| finalString | getString(int arg0,Object... arg1) |
| finalString | getString(int arg0) |
| finalString | getTag() |
| finalFragment | getTargetFragment() |
| final int | getTargetRequestCode() |
| finalCharSequence | getText(int arg0) |
| boolean | getUserVisibleHint() |
| View | getView() |
| final int | hashCode() |
| staticFragment | |
| staticFragment | |
| final boolean | isAdded() |
| final boolean | isDetached() |
| final boolean | isHidden() |
| final boolean | isInLayout() |
| final boolean | isRemoving() |
| final boolean | isResumed() |
| final boolean | isStateSaved() |
| final boolean | isVisible() |
| void | onActivityCreated(Bundle arg0) |
| void | onActivityResult(int arg0, int arg1,Intent arg2) |
| void | onAttach(Activity arg0) |
| void | onAttach(Context arg0) |
| void | onAttachFragment(Fragment arg0) |
| void | onConfigurationChanged(Configuration arg0) |
| boolean | onContextItemSelected(MenuItem arg0) |
| void | onCreate(Bundle arg0) |
| Animator | onCreateAnimator(int arg0, boolean arg1, int arg2) |
| void | |
| void | |
| View | |
| void | onDestroy() |
| void | onDestroyOptionsMenu() |
| void | onDestroyView() |
| void | onDetach() |
| LayoutInflater | onGetLayoutInflater(Bundle arg0) |
| void | onHiddenChanged(boolean arg0) |
| void | |
| void | |
| void | |
| void | onLowMemory() |
| void | onMultiWindowModeChanged(boolean arg0) |
| void | onMultiWindowModeChanged(boolean arg0,Configuration arg1) |
| boolean | onOptionsItemSelected(MenuItem arg0) |
| void | onOptionsMenuClosed(Menu arg0) |
| void | onPause() |
| void | onPictureInPictureModeChanged(boolean arg0,Configuration arg1) |
| void | onPictureInPictureModeChanged(boolean arg0) |
| void | onPrepareOptionsMenu(Menu arg0) |
| void | onRequestPermissionsResult(int arg0,String[] arg1, int[] arg2) |
| void | onResume() |
| void | onSaveInstanceState(Bundle arg0) |
| void | onStart() |
| void | onStop() |
| void | onTrimMemory(int arg0) |
| void | |
| void | onViewStateRestored(Bundle arg0) |
| void | postponeEnterTransition() |
| void | registerForContextMenu(View arg0) |
| final void | requestPermissions(String[] arg0, int arg1) |
| void | setAllowEnterTransitionOverlap(boolean arg0) |
| void | setAllowReturnTransitionOverlap(boolean arg0) |
| void | setArguments(Bundle arg0) |
| void | setEnterSharedElementCallback(SharedElementCallback arg0) |
| void | setEnterTransition(Transition arg0) |
| void | setExitSharedElementCallback(SharedElementCallback arg0) |
| void | setExitTransition(Transition arg0) |
| void | setHasOptionsMenu(boolean arg0) |
| void | setInitialSavedState(Fragment.SavedState arg0) |
| void | setMenuVisibility(boolean arg0) |
| void | setReenterTransition(Transition arg0) |
| void | setRetainInstance(boolean arg0) |
| void | setReturnTransition(Transition arg0) |
| void | setSharedElementEnterTransition(Transition arg0) |
| void | setSharedElementReturnTransition(Transition arg0) |
| void | setTargetFragment(Fragment arg0, int arg1) |
| void | setUserVisibleHint(boolean arg0) |
| boolean | shouldShowRequestPermissionRationale(String arg0) |
| void | startActivity(Intent arg0) |
| void | |
| void | startActivityForResult(Intent arg0, int arg1) |
| void | |
| void | startIntentSenderForResult(IntentSender arg0, int arg1,Intent arg2, int arg3, int arg4, int arg5,Bundle arg6) |
| void | startPostponedEnterTransition() |
| String | toString() |
| void | unregisterForContextMenu(View arg0) |
| abstract void | onTrimMemory(int arg0) |
| abstract void |
| abstract void | onConfigurationChanged(Configuration arg0) |
| abstract void | onLowMemory() |
Public Constructors
publicMapFragment()
Creates a map fragment. This constructor is public only for use by an inflater. UsenewInstance() to create a MapFragment programmatically.
Public Methods
public voidgetMapAsync(OnMapReadyCallback callback)
Sets a callback object which will be triggered when theGoogleMap 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. Must not benull. |
|---|
public staticMapFragmentnewInstance(GoogleMapOptions options)
Creates a map fragment with the given options.
Parameters
| options | configuration GoogleMapOptions for aGoogleMap, ornull to use the default options. |
|---|
public staticMapFragmentnewInstance()
Creates a map fragment, using default options.
public voidonActivityCreated(Bundle savedInstanceState)
public voidonAttach(Activity activity)
public voidonCreate(Bundle savedInstanceState)
publicViewonCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
public voidonDestroy()
public voidonDestroyView()
public final voidonEnterAmbient(Bundle ambientDetails)
You must call this method from the parent WearableActivity's corresponding method.
public final voidonExitAmbient()
You must call this method from the parent WearableActivity's corresponding method.
public voidonInflate(Activity activity,AttributeSet attrs,Bundle savedInstanceState)
Parse attributes during inflation from a view hierarchy into the arguments we handle.
public voidonLowMemory()
public voidonPause()
public voidonResume()
public voidonSaveInstanceState(Bundle outState)
Provides aBundle to store the state of the Fragment before it gets destroyed. It can later be retrieved when onCreate(Bundle) is called again.
public voidonStart()
public voidonStop()
public voidsetArguments(Bundle args)
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.