This repository contains Kotlin extensions (KTX) for:
It enables you to write more concise, idiomatic Kotlin. Each set of extensions can be used independently or together.
- Kotlin-enabled project
- Kotlin coroutines
- API level 15+
If you are using the Maps SDK through Google Play Services:
dependencies {
// KTX for the Maps SDK for Android library
implementation 'com.google.maps.android:maps-ktx:2.1.1'
// KTX for the Maps SDK for Android Utility Library
implementation 'com.google.maps.android:maps-utils-ktx:2.1.1'
// It is recommended to also include the latest Maps SDK and/or Utility Library versions
// as well to ensure that you have the latest features and bug fixes.
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.maps.android:android-maps-utils:2.1.1'
}
Alternatively, if you are using the Maps SDK through the standalone V3 BETA distribution:
dependencies {
// KTX for the Maps SDK for Android V3 BETA Library
implementation 'com.google.maps.android:maps-v3-ktx:2.1.1'
// KTX for the Maps SDK for Android V3 BETA Utility Library
implementation 'com.google.maps.android:maps-utils-v3-ktx:2.1.1'
// It is recommended to also include the latest Maps SDK and/or Utility Library versions
// as well to ensure that you have the latest features and bug fixes.
implementation 'com.google.android.libraries.maps:maps:2.1.1-beta'
implementation 'com.google.maps.android:android-maps-utils-v3:2.1.1'
}
With this KTX library, you should be able to take advantage of several Kotlin language features such as extension functions, named parameters and default arguments, destructuring declarations, and coroutines.
This repository includes a demo app that illustrates the use of this KTX library.
To run the demo app, you'll have to:
- Get a Maps API key
- Create a file in the
app
directory calledsecure.properties
(this file should NOT be under version control to protect your API key) - Add a single line to
app/secure.properties
that looks likeMAPS_API_KEY=YOUR_API_KEY
, whereYOUR_API_KEY
is the API key you obtained in the first step - Build and run
Adding a Marker
:
Before
GoogleMap googleMap = // ...
LatLng sydney = new LatLng(-33.852, 151.211);
MarkerOptions markerOptions = new MarkerOptions()
.position(Sydney)
.title("Marker in Sydney");
Marker marker = googleMap.addMarker(markerOptions);
After
val googleMap = // ...
val sydney = LatLng(-33.852, 151.211)
val marker = googleMap.addMarker {
position(sydney)
title("Marker in Sydney")
}
Accessing a GoogleMap
instance can be retrieved using coroutines vs. traditional the callback mechanism. The example here demonstrates how you can use this feature alongside with Lifecycle-aware coroutine scopes provided in Android’s Architecture Components. To use this, you'll need to add the following to your build.gradle
dependencies:
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:<latest-version>'
Before
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(new OnMapReadyCallback {
@Override
public void onMapReady(GoogleMap googleMap) {
// Access GoogleMap instance here
}
});
}
After
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
lifecycle.coroutineScope.launchWhenCreated {
val googleMap = mapFragment?.awaitMap()
}
}
Note: The following feature utilizes an experimental coroutine API. To use this, you will have to add the
@OptIn(ExperimentalCoroutinesApi::class)
at the site of its usage as well as the compiler flag-Xopt-in=kotlin.RequiresOptIn
.
Listing to camera events can be collected via Kotlin Flow.
�_Before_
val googleMap = //...
googleMap.setOnCameraIdleListener = { //... }
googleMap.setOnCameraMoveCanceledListener { //... }
googleMap.setOnCameraMoveListener { //... }
googleMap.setOnCameraMoveStartedListener { //... }
After
// To be invoked within a coroutine scope
googleMap.cameraEvents().collect { event ->
when (event) {
is CameraIdleEvent -> //...
is CameraMoveCanceledEvent -> //...
is CameraMoveEvent -> //...
Is CameraMoveStartedEvent -> //...
}
}
Checking if a LatLng
is contained within a Polygon
:
Before
Polygon polygon = // some polygon
LatLng latlng = // some latlng
boolean result = PolygonUtil.containsLocation(latlng, polygon.getPoints(), true);
After
val polygon: Polygon = // some polygon
val latlng: LatLng = // some latlng
val result: Boolean = polygon.contains(latLng)
Creating a GeoJsonLayer
object:
Before
GeoJsonLayer layer = new GeoJsonLayer(
map,
geoJsonFile,
null,
polygonManager,
null,
groundOverlayManager
);
After
val layer = geoJsonLayer(
map = map,
geoJsonFile = geoJsonFile,
polygonManager = polygonManager,
groundOverlayManager = groundOverlayManager
)
Destructuring properties of a Point
:
Before
Point point = new Point(1.0, 2.0);
double x = point.x;
double y = point.y;
After
val point = Point(1.0, 2.0)
val (x, y) = point
You can learn more about all the extensions provided by this library by reading the reference documents.
Encounter an issue while using this library?
If you find a bug or have a feature request, please file an issue. Or, if you'd like to contribute, send us a pull request and refer to our code of conduct.
You can also reach us on our Discord channel.
For more information, check out the detailed guide on the Google Developers site.