MapFragment

  • 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 usinggetMapAsync(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.

public classMapFragment extendsFragment

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"/>
AGoogleMap 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

From interface android.content.ComponentCallbacks2
intTRIM_MEMORY_BACKGROUND
intTRIM_MEMORY_COMPLETE
intTRIM_MEMORY_MODERATE
intTRIM_MEMORY_RUNNING_CRITICAL
intTRIM_MEMORY_RUNNING_LOW
intTRIM_MEMORY_RUNNING_MODERATE
intTRIM_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 theGoogleMap instance is ready to be used.
staticMapFragment
newInstance(GoogleMapOptions options)
Creates a map fragment with the given options.
staticMapFragment
newInstance()
Creates a map fragment, using default options.
void
onActivityCreated(Bundle savedInstanceState)
void
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
void
void
onSaveInstanceState(Bundle outState)
Provides aBundle to store the state of the Fragment before it gets destroyed.
void
void
void

Inherited Method Summary

From class android.app.Fragment
void
dump(String arg0,FileDescriptor arg1,PrintWriter arg2,String[] arg3)
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
instantiate(Context arg0,String arg1)
staticFragment
instantiate(Context arg0,String arg1,Bundle arg2)
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
onCreateContextMenu(ContextMenu arg0,View arg1, ContextMenu.ContextMenuInfo arg2)
void
onCreateOptionsMenu(Menu arg0,MenuInflater arg1)
View
onCreateView(LayoutInflater arg0,ViewGroup arg1,Bundle arg2)
void
onDestroy()
void
onDestroyOptionsMenu()
void
onDestroyView()
void
onDetach()
LayoutInflater
onGetLayoutInflater(Bundle arg0)
void
onHiddenChanged(boolean arg0)
void
onInflate(AttributeSet arg0,Bundle arg1)
void
onInflate(Activity arg0,AttributeSet arg1,Bundle arg2)
void
onInflate(Context arg0,AttributeSet arg1,Bundle arg2)
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
onViewCreated(View arg0,Bundle arg1)
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
startActivity(Intent arg0,Bundle arg1)
void
startActivityForResult(Intent arg0, int arg1)
void
startActivityForResult(Intent arg0, int arg1,Bundle arg2)
void
startIntentSenderForResult(IntentSender arg0, int arg1,Intent arg2, int arg3, int arg4, int arg5,Bundle arg6)
void
startPostponedEnterTransition()
String
toString()
void
unregisterForContextMenu(View arg0)
From class java.lang.Object
Object
clone()
boolean
equals(Object arg0)
void
finalize()
finalClass<?>
getClass()
int
hashCode()
final void
notify()
final void
notifyAll()
String
toString()
final void
wait(long arg0, int arg1)
final void
wait(long arg0)
final void
wait()
From interface android.content.ComponentCallbacks2
abstract void
onTrimMemory(int arg0)
From interface android.view.View.OnCreateContextMenuListener
abstract void
onCreateContextMenu(ContextMenu arg0,View arg1, ContextMenu.ContextMenuInfo arg2)
From interface android.content.ComponentCallbacks
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.
  • TheGoogleMap object provided by the callback is non-null.
Parameters
callbackThe 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
optionsconfiguration 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.