Maps SDK for Android Utility Library
This open-source library contains utilities that are useful for a wide
range of applications using the Google Maps SDK for Android.
You can also find Kotlin extensions for this library in Maps Android KTX.
dependencies {
// Utilities for Maps SDK for Android (requires Google Play Services)
// You do not need to add a separate dependency for the Maps SDK for Android
// since this library builds in the compatible version of the Maps SDK.
implementation 'com.google.maps.android:android-maps-utils:3.11.2'
// Optionally add the Kotlin Extensions (KTX) for full Kotlin language support
// See latest version at https://github.com/googlemaps/android-maps-ktx
// implementation 'com.google.maps.android:maps-utils-ktx:<latest-version>'
}
This repository includes a sample app that illustrates the use of this library.
To run the demo app, ensure you’ve met the requirements above then:
local.properties
in the root project (this file should NOT be under version control to protect your API key)local.properties
that looks like MAPS_API_KEY=YOUR_API_KEY
, where YOUR_API_KEY
is the API key you obtained earlierdebug
variant for the Maps SDK for Android versionSee the documentation for a full list of classes and their methods.
Full guides for using the utilities are published in
Google Maps Platform documentation.
OK
.kotlin
StreetViewUtils.fetchStreetViewData(LatLng(8.1425918, 11.5386121), BuildConfig.MAPS_API_KEY,Source.DEFAULT)
fetchStreetViewData
will return NOT_FOUND
, OK
, ZERO_RESULTS
or REQUEST_DENIED
, depending on the response.Source
is set to Source.DEFAULT
, but you can also specify Source.OUTDOOR
to request outdoor Street View panoramas.Collection
object.java
// Clustering
ClusterManager<ClusterItem> clusterManager = // Initialize ClusterManager - if you're using multiple maps features, use the constructor that passes in Manager objects (see next section)
clusterManager.setOnClusterItemClickListener(item -> {
// Listen for clicks on a cluster item here
return false;
});
clusterManager.setOnClusterClickListener(item -> {
// Listen for clicks on a cluster here
return false;
});
// GeoJson
GeoJsonLayer geoJsonLayer = // Initialize GeoJsonLayer - if you're using multiple maps features, use the constructor that passes in Manager objects (see next section)
geoJsonLayer.setOnFeatureClickListener(feature -> {
// Listen for clicks on GeoJson features here
});
// KML
KmlLayer kmlLayer = // Initialize KmlLayer - if you're using multiple maps features, use the constructor that passes in Manager objects (see next section)
kmlLayer.setOnFeatureClickListener(feature -> {
// Listen for clicks on KML features here
});
com.google.maps.android
(e.g. GroundOverlayManager
, MarkerManager
, etc.), say from adding a KML layer, GeoJson layer, or Clustering, you will have to rely on the Collection specific to add an object to the map rather than adding that object directly to GoogleMap
. This is because each Manager sets itself as a click listener so that it can manage click events coming from multiple layers.GroundOverlay
objects:java
GroundOverlayManager groundOverlayManager = // Initialize
// Create a new collection first
GroundOverlayManager.Collection groundOverlayCollection = groundOverlayManager.newCollection();
// Add a new ground overlay
GroundOverlayOptions options = // ...
groundOverlayCollection.addGroundOverlay(options);
java
GroundOverlayOptions options = // ...
googleMap.addGroundOverlay(options);
Marker
, Circle
, Polyline
, and Polygon
.MarkerManager
, adding an InfoWindowAdapter
and/or an OnInfoWindowClickListener
should be done on the MarkerManager.Collection
object.java
CustomInfoWindowAdapter adapter = // ...
OnInfoWindowClickListener listener = // ...
// Create a new Collection from a MarkerManager
MarkerManager markerManager = // ...
MarkerManager.Collection collection = markerManager.newCollection();
// Set InfoWindowAdapter and OnInfoWindowClickListener
collection.setInfoWindowAdapter(adapter);
collection.setOnInfoWindowClickListener(listener);
// Alternatively, if you are using clustering
ClusterManager<ClusterItem> clusterManager = // ...
MarkerManager.Collection markerCollection = clusterManager.getMarkerCollection();
markerCollection.setInfoWindowAdapter(adapter);
markerCollection.setOnInfoWindowClickListener(listener);
java
CustomInfoWindowAdapter adapter = // ...
OnInfoWindowClickListener listener = // ...
googleMap.setInfoWindowAdapter(adapter);
googleMap.setOnInfoWindowClickListener(listener);
MarkerManager
, adding an OnMarkerDragListener
should be done on the MarkerManager.Collection
object.java
// Create a new Collection from a MarkerManager
MarkerManager markerManager = // ...
MarkerManager.Collection collection = markerManager.newCollection();
// Add markers to collection
MarkerOptions markerOptions = // ...
collection.addMarker(markerOptions);
// ...
// Set OnMarkerDragListener
GoogleMap.OnMarkerDragListener listener = // ...
collection.setOnMarkerDragListener(listener);
// Alternatively, if you are using clustering
ClusterManager<ClusterItem> clusterManager = // ...
MarkerManager.Collection markerCollection = clusterManager.getMarkerCollection();
markerCollection.setOnMarkerDragListener(listener);
java
// Add markers
MarkerOptions markerOptions = // ...
googleMap.addMarker(makerOptions);
// Add listener
GoogleMap.OnMarkerDragListener listener = // ...
googleMap.setOnMarkerDragListener(listener);
ClusterManager
.java
clusterManager.clearItems();
clusterManager.addItems(items);
clusterManager.cluster();
DefaultClusterRenderer
), you must override two additional methods in v1:onClusterItemUpdated()
- should be the same as your onBeforeClusterItemRendered()
methodonClusterUpdated()
- should be the same as your onBeforeClusterRendered()
methodMarker
instead of MarkerOptions
*CustomMarkerClusteringDemoActivity
in the demo app for a complete example._Old_
javaContributions are welcome and encouraged! If you’d like to contribute, send us a pull request and refer to our code of conduct and contributing guide.
This library uses Google Maps Platform services. Use of Google Maps Platform services through this library is subject to the Google Maps Platform Terms of Service.
This library is not a Google Maps Platform Core Service. Therefore, the Google Maps Platform Terms of Service (e.g. Technical Support Services, Service Level Agreements, and Deprecation Policy) do not apply to the code in this library.
This library is offered via an open source license. It is not governed by the Google Maps Platform Support [Technical Support Services Guidelines, the SLA, or the Deprecation Policy. However, any Google Maps Platform services used by the library remain subject to the Google Maps Platform Terms of Service.
This library adheres to semantic versioning to indicate when backwards-incompatible changes are introduced. Accordingly, while the library is in version 0.x, backwards-incompatible changes may be introduced at any time.
If you find a bug, or have a feature request, please file an issue on GitHub. If you would like to get answers to technical questions from other Google Maps Platform developers, ask through one of our developer community channels. If you’d like to contribute, please check the contributing guide.
You can also discuss this library on our Discord server.