Stay organized with collections Save and categorize content based on your preferences.
This is the main class of the Google Maps SDK for Android and is the entry point for all methods related to the map. You cannot instantiate a GoogleMap
object directly, rather, you must obtain one from the getMapAsync()
method on a MapFragment
or MapView
that you have added to your application.
Note: Similar to a View
object, a GoogleMap
can only be read and modified from the Android UI thread. Calling GoogleMap
methods from another thread will result in an exception.
You can adjust the viewpoint of a map by changing the position of the camera (as opposed to moving the map). You can use the map's camera to set parameters such as location, zoom level, tilt angle, and bearing. For more information, see Camera and View.
Developer GuideTo get started, read the Google Maps SDK for Android developer guide.
Nested Class Summary Constant Summary Public Method Summary final Circle final GroundOverlay final Marker final void final Polygon final Polyline final TileOverlay final void final void animateCamera(
CameraUpdateupdate)
Animates the movement of the camera from the current position to the position defined in the update.
final void final void clear()
Removes all markers, polylines, polygons, overlays, etc from the map.
final CameraPosition FeatureLayer IndoorBuilding MapCapabilities int final int getMapType()
Gets the type of map that's currently displayed.
final float getMaxZoomLevel()
Returns the maximum zoom level for the current camera position.
final float final Location getMyLocation()
This method is deprecated. Use com.google.android.gms.location.FusedLocationProviderApi instead. FusedLocationProviderApi provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example code, or the Location Developer Guide. final Projection getProjection()
Returns a
Projection
object that you can use to convert between screen coordinates and latitude/longitude coordinates.
final UiSettings final boolean final boolean final boolean final boolean final void final void void final void final void final boolean final void void final void void setMapColorScheme(int mapColorScheme)
Sets the map rendering color scheme to light, dark, or follow system settings after initialization.
boolean final void setMapType(int type)
Sets the type of map tiles that should be displayed.
void void final void final void final void final void final void final void final void final void final void final void final void final void final void void final void final void final void final void final void final void final void final void final void final void setPadding(int left, int top, int right, int bottom)
Sets padding on the map.
final void final void final void final void stopAnimation()
Stops the camera animation if there is one in progress.
Inherited Method Summary From class java.lang.Object Objectclone()
boolean voidfinalize()
final Class<?>getClass()
inthashCode()
final voidnotify()
final voidnotifyAll()
StringtoString()
final voidwait(long arg0, int arg1)
final voidwait(long arg0)
final voidwait()
Constants public static final String DEMO_MAP_IDReturns the DEMO_MAP_ID
, which can be used for code samples which require a Map ID. This Map ID is not intended for use in production applications and cannot be used for features which require cloud configuration (such as Cloud-based maps styling).
Note: Usage of DEMO_MAP_ID
triggers a map load charge against the Dynamic Maps SKU for Android and iOS. For more information, see Google Maps Platform Pricing
Constant Value: "demo_map_id"
public static final int MAP_TYPE_HYBRIDSatellite maps with a transparent layer of major streets.
Constant Value: 4
public static final int MAP_TYPE_NONENo base map tiles.
Constant Value: 0
public static final int MAP_TYPE_NORMALBasic maps.
Constant Value: 1
public static final int MAP_TYPE_SATELLITESatellite maps with no labels.
Constant Value: 2
public static final int MAP_TYPE_TERRAINTerrain maps.
Constant Value: 3
Public Methods public final Circle addCircle (CircleOptions options)Add a circle to this map.
Parameters options A circle options object that defines how to render the Circle ReturnsCircle
object that is added to the mapAdds an image to this map.
Parameters options A ground-overlay options object that defines how to render the overlay. Should never be null. Options must have an image (AnchoredBitmap) and position specified. ReturnsGroundOverlay
that was added to the map. Might be null if there's an error adding the ground overlay.Adds a marker to this map.
The marker's icon is rendered on the map at the location Marker.position. Clicking the marker centers the camera on the marker. If Marker.title is defined, the map shows an info box with the marker's title and snippet. If the marker is draggable, long-clicking and then dragging the marker moves it.
If an AdvancedMarkerOptions
is passed to this method then an AdvancedMarker
will be created and returned as a Marker.
Marker
that was added to the map. Might be null if there's an error adding the marker.Adds a polygon to this map.
Parameters options A polygon options object that defines how to render the Polygon. ReturnsPolygon
object that is added to the map.Adds a polyline to this map.
Parameters options A polyline options object that defines how to render the Polyline. ReturnsPolyline
object that was added to the map.Adds a tile overlay to this map. See TileOverlay
for more information.
Note that unlike other overlays, if the map is recreated, tile overlays are not automatically restored and must be re-added manually.
Parameters options A tile-overlay options object that defines how to render the overlay. Should never be null. Options must have aTileProvider
specified, otherwise an IllegalArgumentException
will be thrown. Returns
TileOverlay
that was added to the map. Might be null if there's an error adding the tile overlay.Animates the movement of the camera from the current position to the position defined in the update. During the animation, a call to getCameraPosition()
returns an intermediate location of the camera.
See CameraUpdateFactory
for a set of updates.
Moves the map according to the update with an animation over a specified duration, and calls an optional callback on completion. See CameraUpdateFactory
for a set of updates.
If getCameraPosition()
is called during the animation, it will return the current location of the camera in flight.
IllegalArgumentException
will be thrown. callback An optional callback to be notified from the Android UI thread when the animation stops. If the animation stops due to its natural completion, the callback will be notified with GoogleMap.CancelableCallback.onFinish()
. If the animation stops due to interruption by a later camera movement or a user gesture, GoogleMap.CancelableCallback.onCancel()
will be called. The callback should not attempt to move or animate the camera in its cancellation method. If a callback isn't required, leave it as null
. public final void clear ()
Removes all markers, polylines, polygons, overlays, etc from the map.
public final CameraPosition getCameraPosition ()Gets the current position of the camera.
The CameraPosition
returned is a snapshot of the current position, and will not automatically update when the camera moves.
For more information about changing the camera's position, see Camera and View.
ReturnsReturns a feature layer of the specified type. Feature layers must be configured in the Cloud Console.
If a layer of the specified type does not exist on this map, or if data-driven styling is not enabled, the resulting layer's FeatureLayer.isAvailable()
will be false, and will not respond to any calls.
Gets the currently focused building.
Returnsnull
if no building is focused.Returns the maximum zoom level for the current camera position. This takes into account what map type is currently being used, e.g., satellite or terrain may have a lower max zoom level than the base map tiles.
Note: This returns the maximum zoom level supported by the Google Maps API for the current camera position, and not the zoom level preference previously requested in setMaxZoomPreference(float)
.
For more information about changing the camera's zoom level, see Camera and View.
ReturnsReturns the minimum zoom level. This is the same for every location (unlike the maximum zoom level) but may vary between devices and map sizes.
Note: This returns the minimum zoom level supported by the Google Maps API, and not the zoom level preference previously requested in setMinZoomPreference(float)
.
For more information about changing the camera's zoom level, see Camera and View.
ReturnsThis method is deprecated.
Use com.google.android.gms.location.FusedLocationProviderApi instead. FusedLocationProviderApi provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example code, or the Location Developer Guide.
Returns the currently displayed user location, or null
if there is no location data available.
Returns a Projection
object that you can use to convert between screen coordinates and latitude/longitude coordinates.
The Projection
returned is a snapshot of the current projection, and will not automatically update when the camera moves. As this operation is expensive, you should get the projection only once per screen. Google Maps uses the Mercator projection to create its maps from geographic data and convert points on the map into geographic coordinates.
Projection
of the map in its current state.Gets the user interface settings for the map.
public final boolean isBuildingsEnabled ()Returns whether 3D buildings layer is enabled.
Returnstrue
if buildings are enabled; false
otherwise.Gets whether indoor maps are currently enabled.
Returnstrue
if indoor maps are enabled; false
if indoor maps are disabled;Gets the status of the my-location layer.
Returnstrue
if the my-location layer is enabled; false
otherwise.Checks whether the map is drawing traffic data. This is subject to the availability of traffic data.
Returnstrue
if traffic data is enabled; false
otherwise.Repositions the camera according to the instructions defined in the update. The move is instantaneous, and a subsequent getCameraPosition()
will reflect the new position. See CameraUpdateFactory
for a set of updates.
Removes any previously specified upper and lower zoom bounds.
public final void setBuildingsEnabled (boolean enabled)Turns the 3D buildings layer on or off.
Parameters enabledtrue
to enable the 3D buildings layer; false
to disable 3D buildings. public final void setContentDescription (String description)
Sets a contentDescription for the map.
This is used to provide a spoken description of the map in accessibility mode. The default value is "Google Map"
Parameters description a string to use as a description. public final boolean setIndoorEnabled (boolean enabled)Sets whether indoor maps should be enabled. Currently, indoor maps can only be shown on one map at a time and by default, this is the first map added to your application. To enable indoor maps on another map, you must first disable indoor maps on the original map. If you try to enable indoor maps when it is enabled on another map, nothing will happen and this will return false
. When Indoor is not enabled for a map, all methods related to indoor will return null
, or false
.
true
to try to enable indoor maps; false
to disable indoor maps. Returns
Sets a custom renderer for the contents of info windows.
Like the map's event listeners, this state is not serialized with the map. If the map gets re-created (e.g., due to a configuration change), you must ensure that you call this method again in order to preserve the customization.
Parameters adapter The adapter to use for info window contents, ornull
to use the default content rendering in info windows. public void setLatLngBoundsForCameraTarget (LatLngBounds bounds)
Specifies a LatLngBounds to constrain the camera target, so that when users scroll and pan the map, the camera target does not move outside these bounds.
Set to null to clear the bounding box completely. The new bounds replaces any previously specified bounding box.
When the LatLngBounds changes, the SDK adjusts all later camera updates to respect those bounds if possible. Note that there are technical considerations that may prevent the SDK from keeping the camera target strictly within the bounds. For example, floating point precision rounding errors or a very low zoom level.
Parameters bounds The bounds to constrain the camera target within. public final void setLocationSource (LocationSource source)Replaces the location source of the my-location layer.
Parameters source A location source to use in the my-location layer. Set tonull
to use the default location source. public void setMapColorScheme (int mapColorScheme) Parameters mapColorScheme to denote which pre-selected color scheme. public boolean setMapStyle (MapStyleOptions style)
Sets the styling of the base map.
Using the style options, you can apply custom styles to features and elements on the map. See MapStyleOptions
for style definition details.
true
if the style was successfully parsed; false
if problems were detected with the MapStyleOptions
, including, e.g. unparsable styling JSON, unrecognized feature type, unrecognized element type, or invalid styler keys. If the return value is false
, the current style is left unchanged.Sets a preferred upper bound for the camera zoom.
When the maximum zoom changes, the SDK adjusts all later camera updates to respect that maximum if possible. Note that there are technical considerations that may prevent the SDK from allowing users to zoom too deep into the map. For example, satellite or terrain may have a lower maximum zoom than the base map tiles.
The SDK resolves any conflicts in the minimum and maximum values. If you set the minimum zoom to a higher zoom level than the current maximum, then the SDK uses the new minimum value for both minimum and maximum. If you set the maximum zoom to a lower zoom level than the minimum, the SDK uses the new maximum value for both minimum and maximum. For example: Assume the current minimum zoom is 8 and the maximum is 14. Then you set the maximum zoom to 6. The SDK uses a minimum zoom of 6 and a maximum zoom of 6.
Parameters maxZoomPreference The preferred upper bound. public void setMinZoomPreference (float minZoomPreference)Sets a preferred lower bound for the camera zoom.
When the minimum zoom changes, the SDK adjusts all later camera updates to respect that minimum if possible. Note that there are technical considerations that may prevent the SDK from allowing users to zoom too low.
The SDK resolves any conflicts in the minimum and maximum values. If you set the minimum zoom to a higher zoom level than the current maximum, then the SDK uses the new minimum value for both minimum and maximum. If you set the maximum zoom to a lower zoom level than the minimum, the SDK uses the new maximum value for both minimum and maximum. For example: Assume the current minimum zoom is 8 and the maximum is 14. Then you set the minimum zoom to 16. The SDK uses a minimum zoom of 16 and a maximum zoom of 16.
Parameters minZoomPreference The preferred lower bound. public final void setMyLocationEnabled (boolean enabled)Enables or disables the my-location layer.
While enabled and the location is available, the my-location layer continuously draws an indication of a user's current location and bearing, and displays UI controls that allow a user to interact with their location (for example, to enable or disable camera tracking of their location and bearing).
In order to use the my-location-layer feature you need to request permission for either ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
unless you have set a custom location source.
If you have set a custom location source via setLocationSource(LocationSource)
the Google Maps SDK for Android will not check whether the above permissions have been granted. However, you still need to make sure that the user has granted all the permissions required for your custom location source.
true
to enable; false
to disable. public final void setOnCameraIdleListener (GoogleMap.OnCameraIdleListener listener)
Sets a callback that is invoked when camera movement has ended.
Parameters listener The callback that's invoked. To unset the callback, usenull
. public final void setOnCameraMoveCanceledListener (GoogleMap.OnCameraMoveCanceledListener listener)
Sets a callback that is invoked when camera motion is stopped or interrupted by a new type of animation.
Parameters listener The callback that's invoked. To unset the callback, usenull
. Do not update or animate the camera from within onCameraMoveCanceled()
. public final void setOnCameraMoveListener (GoogleMap.OnCameraMoveListener listener)
Sets a callback that is invoked repeatedly while the camera is in motion. The callback may be invoked as often as once every frame and should not perform expensive operations.
Parameters listener The callback that's invoked. To unset the callback, usenull
. public final void setOnCameraMoveStartedListener (GoogleMap.OnCameraMoveStartedListener listener)
Sets a callback that's invoked when the camera starts moving or the reason for camera motion has changed.
Parameters listener The callback that's invoked. To unset the callback, usenull
. Do not update or animate the camera from within onCameraMoveStarted()
. public final void setOnCircleClickListener (GoogleMap.OnCircleClickListener listener)
Sets a callback that's invoked when a circle is clicked.
Parameters listener The callback that's invoked when a circle is clicked. To unset the callback, usenull
. public final void setOnGroundOverlayClickListener (GoogleMap.OnGroundOverlayClickListener listener)
Sets a callback that's invoked when a ground overlay is clicked.
Parameters listener The callback that's invoked when a ground overlay is clicked. To unset the callback, usenull
. public final void setOnIndoorStateChangeListener (GoogleMap.OnIndoorStateChangeListener listener)
Sets or clears the listener for indoor events. Only one listener can ever be set. Setting a new listener will remove the previous listener.
Parameters listener the listener for indoor events if non-null; otherwise, clears the listener public final void setOnInfoWindowClickListener (GoogleMap.OnInfoWindowClickListener listener)Sets a callback that's invoked when a marker info window is clicked.
Parameters listener The callback that's invoked when a marker info window is clicked. To unset the callback, usenull
. public final void setOnInfoWindowCloseListener (GoogleMap.OnInfoWindowCloseListener listener)
Sets a callback that's invoked when a marker's info window is closed.
Parameters listener The callback that's invoked when a marker's info window is closed. To unset the callback, usenull
. public final void setOnInfoWindowLongClickListener (GoogleMap.OnInfoWindowLongClickListener listener)
Sets a callback that's invoked when a marker's info window is long pressed.
Parameters listener The callback that's invoked when a marker's info window is long pressed. To unset the callback, usenull
. public final void setOnMapClickListener (GoogleMap.OnMapClickListener listener)
Sets a callback that's invoked when the map is tapped.
Parameters listener The callback that's invoked when the map is tapped. To unset the callback, usenull
. public void setOnMapLoadedCallback (GoogleMap.OnMapLoadedCallback callback)
Sets a callback that's invoked when this map has finished rendering. The callback will only be invoked once.
If this method is called when the map is fully rendered, the callback will be invoked immediately. This event will not fire if the map never loads due to connectivity issues, or if the map is continuously changing and never completes loading due to the user constantly interacting with the map.
Parameters callback The callback invoked when the map has finished rendering. To unset the callback, usenull
. public final void setOnMapLongClickListener (GoogleMap.OnMapLongClickListener listener)
Sets a callback that's invoked when the map is long pressed.
Parameters listener The callback that's invoked when the map is long pressed. To unset the callback, usenull
. public final void setOnMarkerClickListener (GoogleMap.OnMarkerClickListener listener)
Sets a callback that's invoked when a marker is clicked.
Parameters listener The callback that's invoked when a marker is clicked. To unset the callback, usenull
. public final void setOnMarkerDragListener (GoogleMap.OnMarkerDragListener listener)
Sets a callback that's invoked when a marker is dragged.
Parameters listener The callback that's invoked on marker drag events. To unset the callback, usenull
. public final void setOnMyLocationButtonClickListener (GoogleMap.OnMyLocationButtonClickListener listener)
Sets a callback that's invoked when the my location button is clicked.
If the listener
returns true
, the event is consumed and the default behavior will not occur. If it returns false
, the default behavior will occur (i.e. The camera moves such that it is centered on the user's location).
This method is deprecated.
use com.google.android.gms.location.FusedLocationProviderApi instead. FusedLocationProviderApi provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example code, or the Location Developer Guide.
Sets a callback that's invoked when the My Location dot changes location.
Parameters listener The callback that's invoked when the My Location dot changes. public final void setOnMyLocationClickListener (GoogleMap.OnMyLocationClickListener listener)Sets a callback that's invoked when the My Location dot (which signifies the user's location) is clicked.
Parameters listener The callback that's invoked when the My Location dot is clicked. public final void setOnPoiClickListener (GoogleMap.OnPoiClickListener listener)Sets a listener which will be triggered when a POI is clicked or tapped.
Parameters listener Listener for POI click and tap events. Passnull
to clear the listener. public final void setOnPolygonClickListener (GoogleMap.OnPolygonClickListener listener)
Sets a callback that's invoked when a polygon is clicked.
Parameters listener The callback that's invoked when a polygon is clicked. To unset the callback, usenull
. public final void setOnPolylineClickListener (GoogleMap.OnPolylineClickListener listener)
Sets a callback that's invoked when a polyline is clicked.
Parameters listener The callback that's invoked when a polyline is clicked. To unset the callback, usenull
. public final void setPadding (int left, int top, int right, int bottom)
Sets padding on the map.
This method allows you to define a visible region on the map, to signal to the map that portions of the map around the edges may be obscured, by setting padding on each of the four edges of the map. Map functions will be adapted to the padding. For example, the zoom controls, compass, copyright notices and Google logo will be moved to fit inside the defined region, camera movements will be relative to the center of the visible region, etc.
Parameters left the number of pixels of padding to be added on the left of the map. top the number of pixels of padding to be added on the top of the map. right the number of pixels of padding to be added on the right of the map. bottom the number of pixels of padding to be added on the bottom of the map. public final void setTrafficEnabled (boolean enabled)Turns the traffic layer on or off.
public final void snapshot (GoogleMap.SnapshotReadyCallback callback, Bitmap bitmap)Takes a snapshot of the map.
This method is equivalent to snapshot(SnapshotReadyCallback)
but lets you provide a preallocated Bitmap
. If the bitmap does not match the current dimensions of the map, another bitmap will be allocated that fits the map's dimensions.
Although in most cases the object passed by the callback method is the same as the one given in the parameter to this method, in some cases the returned object can be different (e.g., if the view's dimensions have changed by the time the snapshot is actually taken). Thus, you should only trust the content of the bitmap passed by the callback method.
Note:
snapshot()
should only be invoked while the underlying UI view/fragment is in the foreground.Takes a snapshot of the map.
You can use snapshots within your application when an interactive map would be difficult, or impossible, to use. For example, images produced with the snapshot()
method can be used to display a thumbnail of the map in your app, or a snapshot in the notification center.
Note:
snapshot()
should only be invoked while the underlying UI view/fragment is in the foreground.Stops the camera animation if there is one in progress. When the method is called, the camera stops moving immediately and remains in that position.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[[["The `GoogleMap` class is the entry point for interacting with Google Maps in Android apps and controls map display, user interaction, and location features."],["Developers can add elements (markers, circles, etc.), manage camera position, customize map style, and handle user interactions using this class."],["Refer to the Google Maps SDK for Android documentation and linked resources for detailed guidance and examples."],["Numerous nested interfaces and constants within the `GoogleMap` class offer granular control over the map and its behavior."],["Some methods and properties may be deprecated, consult the official documentation for up-to-date alternatives."]]],["The `GoogleMap` class, central to the Android Maps SDK, manages map interactions. It's accessed via `getMapAsync()` and must be used on the UI thread. Key actions include adding overlays like circles, markers, and polygons, and manipulating the camera's position with methods like `animateCamera` and `moveCamera`. The class supports diverse map types, styling with `setMapStyle`, and zoom preferences. It provides numerous event listeners for camera movements, clicks on map elements, and info window interactions. Other actions include enabling/disabling features like traffic, buildings, indoor maps, and setting constraints and padding.\n"]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4