/
GoogleMap.kt
574 lines (527 loc) · 18.6 KB
/
GoogleMap.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.google.maps.android.ktx
import android.graphics.Bitmap
import android.location.Location
import androidx.annotation.IntDef
import com.google.android.gms.maps.CameraUpdate
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.CircleOptions
import com.google.android.gms.maps.model.GroundOverlay
import com.google.android.gms.maps.model.GroundOverlayOptions
import com.google.android.gms.maps.model.IndoorBuilding
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.model.PointOfInterest
import com.google.android.gms.maps.model.Polygon
import com.google.android.gms.maps.model.PolygonOptions
import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.TileOverlay
import com.google.android.gms.maps.model.TileOverlayOptions
import com.google.maps.android.ktx.model.circleOptions
import com.google.maps.android.ktx.model.groundOverlayOptions
import com.google.maps.android.ktx.model.markerOptions
import com.google.maps.android.ktx.model.polygonOptions
import com.google.maps.android.ktx.model.polylineOptions
import com.google.maps.android.ktx.model.tileOverlayOptions
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@IntDef(
GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE,
GoogleMap.OnCameraMoveStartedListener.REASON_API_ANIMATION,
GoogleMap.OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION
)
@Retention(AnnotationRetention.SOURCE)
public annotation class MoveStartedReason
public sealed class CameraEvent
public object CameraIdleEvent : CameraEvent()
public object CameraMoveCanceledEvent : CameraEvent()
public object CameraMoveEvent : CameraEvent()
public data class CameraMoveStartedEvent(@MoveStartedReason val reason: Int) : CameraEvent()
/**
* Change event when a marker is dragged. See [GoogleMap.setOnMarkerDragListener]
*/
public sealed class OnMarkerDragEvent {
public abstract val marker: Marker
}
/**
* Event emitted repeatedly while a marker is being dragged.
*/
public data class MarkerDragEvent(public override val marker: Marker) : OnMarkerDragEvent()
/**
* Event emitted when a marker has finished being dragged.
*/
public data class MarkerDragEndEvent(public override val marker: Marker) : OnMarkerDragEvent()
/**
* Event emitted when a marker starts being dragged.
*/
public data class MarkerDragStartEvent(public override val marker: Marker) : OnMarkerDragEvent()
/**
* Change event when the indoor state changes. See [GoogleMap.OnIndoorStateChangeListener]
*/
public sealed class IndoorChangeEvent
/**
* Change event when an indoor building is focused.
* See [GoogleMap.OnIndoorStateChangeListener.onIndoorBuildingFocused]
*/
public object IndoorBuildingFocusedEvent : IndoorChangeEvent()
/**
* Change event when an indoor level is activated.
* See [GoogleMap.OnIndoorStateChangeListener.onIndoorLevelActivated]
*/
public data class IndoorLevelActivatedEvent(val building: IndoorBuilding) : IndoorChangeEvent()
// Since offer() can throw when the channel is closed (channel can close before the
// block within awaitClose), wrap `offer` calls inside `runCatching`.
// See: https://github.com/Kotlin/kotlinx.coroutines/issues/974
internal fun <E> SendChannel<E>.offerCatching(element: E): Boolean {
return runCatching { offer(element) }.getOrDefault(false)
}
/**
* Returns a [Flow] of [CameraEvent] items so that camera movements can be observed. Using this to
* observe camera events will set listeners and thus override existing listeners to
* [GoogleMap.setOnCameraIdleListener], [GoogleMap.setOnCameraMoveCanceledListener],
* [GoogleMap.setOnCameraMoveListener] and [GoogleMap.setOnCameraMoveStartedListener].
*/
@ExperimentalCoroutinesApi
@Deprecated(
message = "Use cameraIdleEvents(), cameraMoveCanceledEvents(), cameraMoveEvents() or cameraMoveStartedEvents",
)
public fun GoogleMap.cameraEvents(): Flow<CameraEvent> =
callbackFlow {
setOnCameraIdleListener {
offerCatching(CameraIdleEvent)
}
setOnCameraMoveCanceledListener {
offerCatching(CameraMoveCanceledEvent)
}
setOnCameraMoveListener {
offerCatching(CameraMoveEvent)
}
setOnCameraMoveStartedListener {
offerCatching(CameraMoveStartedEvent(it))
}
awaitClose {
setOnCameraIdleListener(null)
setOnCameraMoveCanceledListener(null)
setOnCameraMoveListener(null)
setOnCameraMoveStartedListener(null)
}
}
/**
* A suspending function that awaits the completion of the [cameraUpdate] animation.
*
* @param cameraUpdate the [CameraUpdate] to apply on the map
* @param durationMs the duration in milliseconds of the animation. Defaults to 3 seconds.
*/
public suspend inline fun GoogleMap.awaitAnimateCamera(
cameraUpdate: CameraUpdate,
durationMs: Int = 3000
): Unit =
suspendCancellableCoroutine { continuation ->
animateCamera(cameraUpdate, durationMs, object : GoogleMap.CancelableCallback {
override fun onFinish() {
continuation.resume(Unit)
}
override fun onCancel() {
continuation.cancel()
}
})
}
/**
* A suspending function that awaits for the map to be loaded. Uses
* [GoogleMap.setOnMapLoadedCallback].
*/
public suspend inline fun GoogleMap.awaitMapLoad(): Unit =
suspendCoroutine { continuation ->
setOnMapLoadedCallback {
continuation.resume(Unit)
}
}
/**
* Returns a flow that emits when the camera is idle. Using this to observe camera idle events will
* override an existing listener (if any) to [GoogleMap.setOnCameraIdleListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraIdleEvents(): Flow<Unit> =
callbackFlow {
setOnCameraIdleListener {
offerCatching(Unit)
}
awaitClose {
setOnCameraIdleListener(null)
}
}
/**
* Returns a flow that emits when a camera move is canceled. Using this to observe camera move
* cancel events will override an existing listener (if any) to
* [GoogleMap.setOnCameraMoveCanceledListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraMoveCanceledEvents(): Flow<Unit> =
callbackFlow {
setOnCameraMoveCanceledListener {
offerCatching(Unit)
}
awaitClose {
setOnCameraMoveCanceledListener(null)
}
}
/**
* Returns a flow that emits when the camera moves. Using this to observe camera move events will
* override an existing listener (if any) to [GoogleMap.setOnCameraMoveListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraMoveEvents(): Flow<Unit> =
callbackFlow {
setOnCameraMoveListener {
offerCatching(Unit)
}
awaitClose {
setOnCameraMoveListener(null)
}
}
/**
* A suspending function that returns a bitmap snapshot of the current view of the map. Uses
* [GoogleMap.snapshot].
*
* @param bitmap an optional preallocated bitmap
* @return the snapshot
*/
public suspend inline fun GoogleMap.awaitSnapshot(bitmap: Bitmap? = null): Bitmap? =
suspendCoroutine { continuation ->
snapshot({ continuation.resume(it) }, bitmap)
}
/**
* Returns a flow that emits when a camera move started. Using this to observe camera move start
* events will override an existing listener (if any) to [GoogleMap.setOnCameraMoveStartedListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraMoveStartedEvents(): Flow<Unit> =
callbackFlow {
setOnCameraMoveStartedListener {
offerCatching(Unit)
}
awaitClose {
setOnCameraMoveStartedListener(null)
}
}
/**
* Returns a flow that emits when a circle is clicked. Using this to observe circle clicks events
* will override an existing listener (if any) to [GoogleMap.setOnCircleClickListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.circleClickEvents(): Flow<Circle> =
callbackFlow {
setOnCircleClickListener {
offerCatching(it)
}
awaitClose {
setOnCircleClickListener(null)
}
}
/**
* Returns a flow that emits when a ground overlay is clicked. Using this to observe ground overlay
* clicks events will override an existing listener (if any) to
* [GoogleMap.setOnGroundOverlayClickListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.groundOverlayClicks(): Flow<GroundOverlay> =
callbackFlow {
setOnGroundOverlayClickListener {
offerCatching(it)
}
awaitClose {
setOnGroundOverlayClickListener(null)
}
}
/**
* Returns a flow that emits when the indoor state changes. Using this to observe indoor state
* change events will override an existing listener (if any) to
* [GoogleMap.setOnIndoorStateChangeListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.indoorStateChangeEvents(): Flow<IndoorChangeEvent> =
callbackFlow {
setOnIndoorStateChangeListener(object : GoogleMap.OnIndoorStateChangeListener {
override fun onIndoorBuildingFocused() {
offerCatching(IndoorBuildingFocusedEvent)
}
override fun onIndoorLevelActivated(indoorBuilding: IndoorBuilding) {
offerCatching(IndoorLevelActivatedEvent(building = indoorBuilding))
}
})
awaitClose {
setOnIndoorStateChangeListener(null)
}
}
/**
* Returns a flow that emits when a marker's info window is clicked. Using this to observe info
* info window clicks will override an existing listener (if any) to
* [GoogleMap.setOnInfoWindowClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.infoWindowClickEvents(): Flow<Marker> =
callbackFlow {
setOnInfoWindowClickListener {
offerCatching(it)
}
awaitClose {
setOnInfoWindowClickListener(null)
}
}
/**
* Returns a flow that emits when a marker's info window is closed. Using this to observe info
* window closes will override an existing listener (if any) to
* [GoogleMap.setOnInfoWindowCloseListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.infoWindowCloseEvents(): Flow<Marker> =
callbackFlow {
setOnInfoWindowCloseListener {
offerCatching(it)
}
awaitClose {
setOnInfoWindowCloseListener(null)
}
}
/**
* Returns a flow that emits when a marker's info window is long pressed. Using this to observe info
* window long presses will override an existing listener (if any) to
* [GoogleMap.setOnInfoWindowLongClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.infoWindowLongClickEvents(): Flow<Marker> =
callbackFlow {
setOnInfoWindowLongClickListener {
offerCatching(it)
}
awaitClose {
setOnInfoWindowLongClickListener(null)
}
}
/**
* Returns a flow that emits when the map is clicked. Using this to observe map click events will
* override an existing listener (if any) to [GoogleMap.setOnMapClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.mapClickEvents(): Flow<LatLng> =
callbackFlow {
setOnMapClickListener {
offerCatching(it)
}
awaitClose {
setOnMapClickListener(null)
}
}
/**
* Returns a flow that emits when the map is long clicked. Using this to observe map click events
* will override an existing listener (if any) to [GoogleMap.setOnMapLongClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.mapLongClickEvents(): Flow<LatLng> =
callbackFlow {
setOnMapLongClickListener {
offerCatching(it)
}
awaitClose {
setOnMapLongClickListener(null)
}
}
/**
* Returns a flow that emits when a marker on the map is clicked. Using this to observe marker click
* events will override an existing listener (if any) to [GoogleMap.setOnMarkerClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.markerClickEvents(): Flow<Marker> =
callbackFlow {
setOnMarkerClickListener {
offerCatching(it)
}
awaitClose {
setOnMarkerClickListener(null)
}
}
/**
* Returns a flow that emits when a marker is dragged. Using this to observer marker drag events
* will override existing listeners (if any) to [GoogleMap.setOnMarkerDragListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.markerDragEvents(): Flow<OnMarkerDragEvent> =
callbackFlow {
setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener {
override fun onMarkerDragStart(marker: Marker) {
offerCatching(MarkerDragStartEvent(marker = marker))
}
override fun onMarkerDrag(marker: Marker) {
offerCatching(MarkerDragEvent(marker = marker))
}
override fun onMarkerDragEnd(marker: Marker) {
offerCatching(MarkerDragEndEvent(marker = marker))
}
})
awaitClose {
setOnMarkerDragListener(null)
}
}
/**
* Returns a flow that emits when the my location button is clicked. Using this to observe my
* location button click events will override an existing listener (if any) to
* [GoogleMap.setOnMyLocationButtonClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.myLocationButtonClickEvents(): Flow<Unit> =
callbackFlow {
setOnMyLocationButtonClickListener {
offerCatching(Unit)
}
awaitClose {
setOnMyLocationButtonClickListener(null)
}
}
/**
* Returns a flow that emits when the my location blue dot is clicked. Using this to observe my
* location blue dot click events will override an existing listener (if any) to
* [GoogleMap.setOnMyLocationClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.myLocationClickEvents(): Flow<Location> =
callbackFlow {
setOnMyLocationClickListener {
offerCatching(it)
}
awaitClose {
setOnMyLocationClickListener(null)
}
}
/**
* Returns a flow that emits when a PointOfInterest is clicked. Using this to observe
* PointOfInterest click events will override an existing listener (if any) to
* [GoogleMap.setOnPoiClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.poiClickEvents(): Flow<PointOfInterest> =
callbackFlow {
setOnPoiClickListener {
offerCatching(it)
}
awaitClose {
setOnPoiClickListener(null)
}
}
/**
* Returns a flow that emits when a Polygon is clicked. Using this to observe Polygon click events
* will override an existing listener (if any) to [GoogleMap.setOnPolygonClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.polygonClickEvents(): Flow<Polygon> =
callbackFlow {
setOnPolygonClickListener {
offerCatching(it)
}
awaitClose {
setOnPolygonClickListener(null)
}
}
/**
* Returns a flow that emits when a Polyline is clicked. Using this to observe Polyline click events
* will override an existing listener (if any) to [GoogleMap.setOnPolylineClickListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.polylineClickEvents(): Flow<Polyline> =
callbackFlow {
setOnPolylineClickListener {
offerCatching(it)
}
awaitClose {
setOnPolylineClickListener(null)
}
}
/**
* Builds a new [GoogleMapOptions] using the provided [optionsActions].
*
* @return the constructed [GoogleMapOptions]
*/
public inline fun buildGoogleMapOptions(optionsActions: GoogleMapOptions.() -> Unit): GoogleMapOptions =
GoogleMapOptions().apply(
optionsActions
)
/**
* Adds a [Circle] to this [GoogleMap] using the function literal with receiver [optionsActions].
*
* @return the added [Circle]
*/
public inline fun GoogleMap.addCircle(optionsActions: CircleOptions.() -> Unit): Circle =
this.addCircle(
circleOptions(optionsActions)
)
/**
* Adds a [GroundOverlay] to this [GoogleMap] using the function literal with receiver
* [optionsActions].
*
* @return the added [Circle]
*/
public inline fun GoogleMap.addGroundOverlay(optionsActions: GroundOverlayOptions.() -> Unit): GroundOverlay? =
this.addGroundOverlay(
groundOverlayOptions(optionsActions)
)
/**
* Adds a [Marker] to this [GoogleMap] using the function literal with receiver [optionsActions].
*
* @return the added [Marker]
*/
public inline fun GoogleMap.addMarker(optionsActions: MarkerOptions.() -> Unit): Marker? =
this.addMarker(
markerOptions(optionsActions)
)
/**
* Adds a [Polygon] to this [GoogleMap] using the function literal with receiver [optionsActions].
*
* @return the added [Polygon]
*/
public inline fun GoogleMap.addPolygon(optionsActions: PolygonOptions.() -> Unit): Polygon =
this.addPolygon(
polygonOptions(optionsActions)
)
/**
* Adds a [Polyline] to this [GoogleMap] using the function literal with receiver [optionsActions].
*
* @return the added [Polyline]
*/
public inline fun GoogleMap.addPolyline(optionsActions: PolylineOptions.() -> Unit): Polyline =
this.addPolyline(
polylineOptions(optionsActions)
)
/**
* Adds a [TileOverlay] to this [GoogleMap] using the function literal with receiver
* [optionsActions].
*
* @return the added [Polyline]
*/
public inline fun GoogleMap.addTileOverlay(optionsActions: TileOverlayOptions.() -> Unit): TileOverlay? =
this.addTileOverlay(
tileOverlayOptions(optionsActions)
)