-
Notifications
You must be signed in to change notification settings - Fork 131
/
Marker.kt
795 lines (767 loc) · 31.1 KB
/
Marker.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// Copyright 2021 Google LLC
//
// 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.compose
import android.view.View
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ComposeNode
import androidx.compose.runtime.CompositionContext
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCompositionContext
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Offset
import com.google.android.gms.maps.model.AdvancedMarkerOptions
import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.PinConfig
import com.google.maps.android.ktx.addMarker
internal class MarkerNode(
val compositionContext: CompositionContext,
val marker: Marker,
val markerState: MarkerState,
var onMarkerClick: (Marker) -> Boolean,
var onInfoWindowClick: (Marker) -> Unit,
var onInfoWindowClose: (Marker) -> Unit,
var onInfoWindowLongClick: (Marker) -> Unit,
var infoWindow: (@Composable (Marker) -> Unit)?,
var infoContent: (@Composable (Marker) -> Unit)?,
) : MapNode {
override fun onAttached() {
markerState.marker = marker
}
override fun onRemoved() {
markerState.marker = null
marker.remove()
}
override fun onCleared() {
markerState.marker = null
marker.remove()
}
}
@Immutable
@Deprecated("START, DRAG, END are events, not states. Avoid usage.")
public enum class DragState {
START, DRAG, END
}
/**
* A state object that can be hoisted to control and observe the marker state.
*
* This cannot be used to preserve marker info window visibility across configuration changes.
*
* @param position the initial marker position
*/
public class MarkerState(
position: LatLng = LatLng(0.0, 0.0),
) {
/**
* Current position of the marker.
*
* This property is backed by Compose state.
* It can be updated by the API user and by the API itself:
* it has two potentially competing sources of truth.
*
* The API will not update the property unless [isDragging] `== true`,
* which will happen if and only if a Marker is draggable and the end user
* is currently dragging it.
*/
public var position: LatLng by mutableStateOf(position)
/**
* Reflects whether the end user is currently dragging the marker.
* Dragging can happen only if a Marker is draggable.
*
* This property is backed by Compose state.
*/
public var isDragging: Boolean by mutableStateOf(false)
internal set
/**
* Current [DragState] of the marker.
*/
@Deprecated(
"Use isDragging instead - dragState is not appropriate for representing \"state\";" +
" it is a lossy representation of drag \"events\", promoting invalid usage.",
level = DeprecationLevel.WARNING
)
@Suppress("DEPRECATION")
public var dragState: DragState by mutableStateOf(DragState.END)
internal set
// The marker associated with this MarkerState.
private val markerState: MutableState<Marker?> = mutableStateOf(null)
internal var marker: Marker?
get() = markerState.value
set(value) {
if (markerState.value == null && value == null) return
if (markerState.value != null && value != null) {
error("MarkerState may only be associated with one Marker at a time.")
}
markerState.value = value
}
/**
* Shows the info window for the underlying marker.
*
* Not backed by Compose state to accommodate
* [com.google.android.gms.maps.GoogleMap] special semantics:
* only a single info window can be visible for the entire GoogleMap.
*
* Only use from Compose Effect APIs, never directly from composition, to avoid exceptions and
* unexpected behavior from cancelled compositions.
*/
public fun showInfoWindow() {
marker?.showInfoWindow()
}
/**
* Hides the info window for the underlying marker.
*
* Not backed by observable Compose state to accommodate
* [com.google.android.gms.maps.GoogleMap] special semantics:
* only a single info window can be visible for the entire GoogleMap.
*
* Only use from Compose Effect APIs, never directly from composition, to avoid
* unexpected behavior from cancelled compositions.
*/
public fun hideInfoWindow() {
marker?.hideInfoWindow()
}
public companion object {
/**
* The default saver implementation for [MarkerState]
*
* This cannot be used to preserve marker info window visibility across
* configuration changes.
*/
public val Saver: Saver<MarkerState, LatLng> = Saver(
save = { it.position },
restore = { MarkerState(it) }
)
}
}
/**
* Uses [rememberSaveable] to retain [MarkerState.position] across configuration changes,
* for simple use cases.
*
* Other use cases may be better served syncing [MarkerState.position] with a data model.
*
* This cannot be used to preserve info window visibility across configuration changes.
*/
@Composable
public fun rememberMarkerState(
key: String? = null,
position: LatLng = LatLng(0.0, 0.0)
): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) {
MarkerState(position)
}
/**
* A composable for a marker on the map.
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param contentDescription the content description for accessibility purposes
* @param alpha the alpha (opacity) of the marker
* @param anchor the anchor for the marker image
* @param draggable sets the draggability for the marker
* @param flat sets if the marker should be flat against the map
* @param icon sets the icon for the marker
* @param infoWindowAnchor the anchor point of the info window on the marker image
* @param rotation the rotation of the marker in degrees clockwise about the marker's anchor point
* @param snippet the snippet for the marker
* @param tag optional tag to associate with the marker
* @param title the title for the marker
* @param visible the visibility of the marker
* @param zIndex the z-index of the marker
* @param onClick a lambda invoked when the marker is clicked
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
*/
@Composable
@GoogleMapComposable
public fun Marker(
state: MarkerState = rememberMarkerState(),
contentDescription: String? = "",
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
draggable: Boolean = false,
flat: Boolean = false,
icon: BitmapDescriptor? = null,
infoWindowAnchor: Offset = Offset(0.5f, 0.0f),
rotation: Float = 0.0f,
snippet: String? = null,
tag: Any? = null,
title: String? = null,
visible: Boolean = true,
zIndex: Float = 0.0f,
onClick: (Marker) -> Boolean = { false },
onInfoWindowClick: (Marker) -> Unit = {},
onInfoWindowClose: (Marker) -> Unit = {},
onInfoWindowLongClick: (Marker) -> Unit = {},
) {
MarkerImpl(
state = state,
contentDescription = contentDescription,
alpha = alpha,
anchor = anchor,
draggable = draggable,
flat = flat,
icon = icon,
infoWindowAnchor = infoWindowAnchor,
rotation = rotation,
snippet = snippet,
tag = tag,
title = title,
visible = visible,
zIndex = zIndex,
onClick = onClick,
onInfoWindowClick = onInfoWindowClick,
onInfoWindowClose = onInfoWindowClose,
onInfoWindowLongClick = onInfoWindowLongClick,
)
}
/**
* Composable rendering the content passed as a marker.
*
* @param keys unique keys representing the state of this Marker. Any changes to one of the key will
* trigger a rendering of the content composable and thus the rendering of an updated marker.
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param contentDescription the content description for accessibility purposes
* @param alpha the alpha (opacity) of the marker
* @param anchor the anchor for the marker image
* @param draggable sets the draggability for the marker
* @param flat sets if the marker should be flat against the map
* @param infoWindowAnchor the anchor point of the info window on the marker image
* @param rotation the rotation of the marker in degrees clockwise about the marker's anchor point
* @param snippet the snippet for the marker
* @param tag optional tag to associate with the marker
* @param title the title for the marker
* @param visible the visibility of the marker
* @param zIndex the z-index of the marker
* @param onClick a lambda invoked when the marker is clicked
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
* @param content composable lambda expression used to customize the marker's content
*/
@Composable
@GoogleMapComposable
public fun MarkerComposable(
vararg keys: Any,
state: MarkerState = rememberMarkerState(),
contentDescription: String? = "",
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
draggable: Boolean = false,
flat: Boolean = false,
infoWindowAnchor: Offset = Offset(0.5f, 0.0f),
rotation: Float = 0.0f,
snippet: String? = null,
tag: Any? = null,
title: String? = null,
visible: Boolean = true,
zIndex: Float = 0.0f,
onClick: (Marker) -> Boolean = { false },
onInfoWindowClick: (Marker) -> Unit = {},
onInfoWindowClose: (Marker) -> Unit = {},
onInfoWindowLongClick: (Marker) -> Unit = {},
content: @Composable () -> Unit,
) {
val icon = rememberComposeBitmapDescriptor(*keys) { content() }
MarkerImpl(
state = state,
contentDescription = contentDescription,
alpha = alpha,
anchor = anchor,
draggable = draggable,
flat = flat,
icon = icon,
infoWindowAnchor = infoWindowAnchor,
rotation = rotation,
snippet = snippet,
tag = tag,
title = title,
visible = visible,
zIndex = zIndex,
onClick = onClick,
onInfoWindowClick = onInfoWindowClick,
onInfoWindowClose = onInfoWindowClose,
onInfoWindowLongClick = onInfoWindowLongClick,
)
}
/**
* A composable for a marker on the map wherein its entire info window can be
* customized. If this customization is not required, use
* [com.google.maps.android.compose.Marker].
*
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param alpha the alpha (opacity) of the marker
* @param anchor the anchor for the marker image
* @param draggable sets the draggability for the marker
* @param flat sets if the marker should be flat against the map
* @param icon sets the icon for the marker
* @param infoWindowAnchor the anchor point of the info window on the marker image
* @param rotation the rotation of the marker in degrees clockwise about the marker's anchor point
* @param snippet the snippet for the marker
* @param tag optional tag to associate with the marker
* @param title the title for the marker
* @param visible the visibility of the marker
* @param zIndex the z-index of the marker
* @param onClick a lambda invoked when the marker is clicked
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
* @param content optional composable lambda expression for customizing the
* info window's content
*/
@Composable
@GoogleMapComposable
public fun MarkerInfoWindow(
state: MarkerState = rememberMarkerState(),
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
draggable: Boolean = false,
flat: Boolean = false,
icon: BitmapDescriptor? = null,
infoWindowAnchor: Offset = Offset(0.5f, 0.0f),
rotation: Float = 0.0f,
snippet: String? = null,
tag: Any? = null,
title: String? = null,
visible: Boolean = true,
zIndex: Float = 0.0f,
onClick: (Marker) -> Boolean = { false },
onInfoWindowClick: (Marker) -> Unit = {},
onInfoWindowClose: (Marker) -> Unit = {},
onInfoWindowLongClick: (Marker) -> Unit = {},
content: (@Composable (Marker) -> Unit)? = null
) {
MarkerImpl(
state = state,
alpha = alpha,
anchor = anchor,
draggable = draggable,
flat = flat,
icon = icon,
infoWindowAnchor = infoWindowAnchor,
rotation = rotation,
snippet = snippet,
tag = tag,
title = title,
visible = visible,
zIndex = zIndex,
onClick = onClick,
onInfoWindowClick = onInfoWindowClick,
onInfoWindowClose = onInfoWindowClose,
onInfoWindowLongClick = onInfoWindowLongClick,
infoWindow = content,
)
}
/**
* A composable for a marker on the map wherein its info window contents can be
* customized. If this customization is not required, use
* [com.google.maps.android.compose.Marker].
*
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param alpha the alpha (opacity) of the marker
* @param anchor the anchor for the marker image
* @param draggable sets the draggability for the marker
* @param flat sets if the marker should be flat against the map
* @param icon sets the icon for the marker
* @param infoWindowAnchor the anchor point of the info window on the marker image
* @param rotation the rotation of the marker in degrees clockwise about the marker's anchor point
* @param snippet the snippet for the marker
* @param tag optional tag to associate with the marker
* @param title the title for the marker
* @param visible the visibility of the marker
* @param zIndex the z-index of the marker
* @param onClick a lambda invoked when the marker is clicked
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
* @param content optional composable lambda expression for customizing the
* info window's content
*/
@Composable
@GoogleMapComposable
public fun MarkerInfoWindowContent(
state: MarkerState = rememberMarkerState(),
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
draggable: Boolean = false,
flat: Boolean = false,
icon: BitmapDescriptor? = null,
infoWindowAnchor: Offset = Offset(0.5f, 0.0f),
rotation: Float = 0.0f,
snippet: String? = null,
tag: Any? = null,
title: String? = null,
visible: Boolean = true,
zIndex: Float = 0.0f,
onClick: (Marker) -> Boolean = { false },
onInfoWindowClick: (Marker) -> Unit = {},
onInfoWindowClose: (Marker) -> Unit = {},
onInfoWindowLongClick: (Marker) -> Unit = {},
content: (@Composable (Marker) -> Unit)? = null
) {
MarkerImpl(
state = state,
alpha = alpha,
anchor = anchor,
draggable = draggable,
flat = flat,
icon = icon,
infoWindowAnchor = infoWindowAnchor,
rotation = rotation,
snippet = snippet,
tag = tag,
title = title,
visible = visible,
zIndex = zIndex,
onClick = onClick,
onInfoWindowClick = onInfoWindowClick,
onInfoWindowClose = onInfoWindowClose,
onInfoWindowLongClick = onInfoWindowLongClick,
infoContent = content,
)
}
/**
* Internal implementation for a marker on a Google map.
*
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param contentDescription the content description for accessibility purposes
* @param alpha the alpha (opacity) of the marker
* @param anchor the anchor for the marker image
* @param draggable sets the draggability for the marker
* @param flat sets if the marker should be flat against the map
* @param icon sets the icon for the marker
* @param infoWindowAnchor the anchor point of the info window on the marker image
* @param rotation the rotation of the marker in degrees clockwise about the marker's anchor point
* @param snippet the snippet for the marker
* @param tag optional tag to associate with the marker
* @param title the title for the marker
* @param visible the visibility of the marker
* @param zIndex the z-index of the marker
* @param onClick a lambda invoked when the marker is clicked
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
* @param infoWindow optional composable lambda expression for customizing
* the entire info window. If this value is non-null, the value in infoContent]
* will be ignored.
* @param infoContent optional composable lambda expression for customizing
* the info window's content. If this value is non-null, [infoWindow] must be null.
*/
@Composable
@GoogleMapComposable
private fun MarkerImpl(
state: MarkerState = rememberMarkerState(),
contentDescription: String? = "",
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
draggable: Boolean = false,
flat: Boolean = false,
icon: BitmapDescriptor? = null,
infoWindowAnchor: Offset = Offset(0.5f, 0.0f),
rotation: Float = 0.0f,
snippet: String? = null,
tag: Any? = null,
title: String? = null,
visible: Boolean = true,
zIndex: Float = 0.0f,
onClick: (Marker) -> Boolean = { false },
onInfoWindowClick: (Marker) -> Unit = {},
onInfoWindowClose: (Marker) -> Unit = {},
onInfoWindowLongClick: (Marker) -> Unit = {},
infoWindow: (@Composable (Marker) -> Unit)? = null,
infoContent: (@Composable (Marker) -> Unit)? = null,
) {
val mapApplier = currentComposer.applier as? MapApplier
val compositionContext = rememberCompositionContext()
ComposeNode<MarkerNode, MapApplier>(
factory = {
val marker = mapApplier?.map?.addMarker {
contentDescription(contentDescription)
alpha(alpha)
anchor(anchor.x, anchor.y)
draggable(draggable)
flat(flat)
icon(icon)
infoWindowAnchor(infoWindowAnchor.x, infoWindowAnchor.y)
position(state.position)
rotation(rotation)
snippet(snippet)
title(title)
visible(visible)
zIndex(zIndex)
} ?: error("Error adding marker")
marker.tag = tag
MarkerNode(
compositionContext = compositionContext,
marker = marker,
markerState = state,
onMarkerClick = onClick,
onInfoWindowClick = onInfoWindowClick,
onInfoWindowClose = onInfoWindowClose,
onInfoWindowLongClick = onInfoWindowLongClick,
infoContent = infoContent,
infoWindow = infoWindow,
)
},
update = {
update(onClick) { this.onMarkerClick = it }
update(onInfoWindowClick) { this.onInfoWindowClick = it }
update(onInfoWindowClose) { this.onInfoWindowClose = it }
update(onInfoWindowLongClick) { this.onInfoWindowLongClick = it }
update(infoContent) { this.infoContent = it }
update(infoWindow) { this.infoWindow = it }
update(alpha) { this.marker.alpha = it }
update(anchor) { this.marker.setAnchor(it.x, it.y) }
update(draggable) { this.marker.isDraggable = it }
update(flat) { this.marker.isFlat = it }
update(icon) { this.marker.setIcon(it) }
update(infoWindowAnchor) { this.marker.setInfoWindowAnchor(it.x, it.y) }
update(state.position) { this.marker.position = it }
update(rotation) { this.marker.rotation = it }
update(snippet) {
this.marker.snippet = it
if (this.marker.isInfoWindowShown) {
this.marker.showInfoWindow()
}
}
update(tag) { this.marker.tag = it }
update(title) {
this.marker.title = it
if (this.marker.isInfoWindowShown) {
this.marker.showInfoWindow()
}
}
update(visible) { this.marker.isVisible = it }
update(zIndex) { this.marker.zIndex = it }
}
)
}
/**
* A composable for an advanced marker on the map.
*
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param contentDescription the content description for accessibility purposes
* @param alpha the alpha (opacity) of the marker
* @param anchor the anchor for the marker image
* @param draggable sets the draggability for the marker
* @param flat sets if the marker should be flat against the map
* @param infoWindowAnchor the anchor point of the info window on the marker image
* @param rotation the rotation of the marker in degrees clockwise about the marker's anchor point
* @param snippet the snippet for the marker
* @param tag optional tag to associate with the marker
* @param title the title for the marker
* @param visible the visibility of the marker
* @param zIndex the z-index of the marker
* @param onClick a lambda invoked when the marker is clicked
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
* @param pinConfig the PinConfig object that will be used for the advanced marker
* @param iconView the custom view to be used on the advanced marker
* @param collisionBehavior the expected collision behavior
*/
@Composable
@GoogleMapComposable
public fun AdvancedMarker(
state: MarkerState = rememberMarkerState(),
contentDescription: String? = "",
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
draggable: Boolean = false,
flat: Boolean = false,
infoWindowAnchor: Offset = Offset(0.5f, 0.0f),
rotation: Float = 0.0f,
snippet: String? = null,
tag: Any? = null,
title: String? = null,
visible: Boolean = true,
zIndex: Float = 0.0f,
onClick: (Marker) -> Boolean = { false },
onInfoWindowClick: (Marker) -> Unit = {},
onInfoWindowClose: (Marker) -> Unit = {},
onInfoWindowLongClick: (Marker) -> Unit = {},
pinConfig: PinConfig? = null,
iconView: View? = null,
collisionBehavior: Int = AdvancedMarkerOptions.CollisionBehavior.REQUIRED
) {
AdvancedMarkerImpl(
state = state,
contentDescription = contentDescription,
alpha = alpha,
anchor = anchor,
draggable = draggable,
flat = flat,
infoWindowAnchor = infoWindowAnchor,
rotation = rotation,
snippet = snippet,
tag = tag,
title = title,
visible = visible,
zIndex = zIndex,
onClick = onClick,
onInfoWindowClick = onInfoWindowClick,
onInfoWindowClose = onInfoWindowClose,
onInfoWindowLongClick = onInfoWindowLongClick,
pinConfig = pinConfig,
iconView = iconView,
collisionBehavior = collisionBehavior
)
}
/**
* Internal implementation for an advanced marker on a Google map.
*
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param contentDescription the content description for accessibility purposes
* @param alpha the alpha (opacity) of the marker
* @param anchor the anchor for the marker image
* @param draggable sets the draggability for the marker
* @param flat sets if the marker should be flat against the map
* @param infoWindowAnchor the anchor point of the info window on the marker image
* @param rotation the rotation of the marker in degrees clockwise about the marker's anchor point
* @param snippet the snippet for the marker
* @param tag optional tag to associate with the marker
* @param title the title for the marker
* @param visible the visibility of the marker
* @param zIndex the z-index of the marker
* @param onClick a lambda invoked when the marker is clicked
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
* @param infoWindow optional composable lambda expression for customizing
* the entire info window. If this value is non-null, the value in infoContent]
* will be ignored.
* @param infoContent optional composable lambda expression for customizing
* the info window's content. If this value is non-null, [infoWindow] must be null.
* @param pinConfig the PinConfig object that will be used for the advanced marker
* @param iconView the custom view to be used on the advanced marker
* @param collisionBehavior the expected collision behavior
*/
@Composable
@GoogleMapComposable
private fun AdvancedMarkerImpl(
state: MarkerState = rememberMarkerState(),
contentDescription: String? = "",
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
draggable: Boolean = false,
flat: Boolean = false,
infoWindowAnchor: Offset = Offset(0.5f, 0.0f),
rotation: Float = 0.0f,
snippet: String? = null,
tag: Any? = null,
title: String? = null,
visible: Boolean = true,
zIndex: Float = 0.0f,
onClick: (Marker) -> Boolean = { false },
onInfoWindowClick: (Marker) -> Unit = {},
onInfoWindowClose: (Marker) -> Unit = {},
onInfoWindowLongClick: (Marker) -> Unit = {},
infoWindow: (@Composable (Marker) -> Unit)? = null,
infoContent: (@Composable (Marker) -> Unit)? = null,
pinConfig: PinConfig? = null,
iconView: View? = null,
collisionBehavior: Int = AdvancedMarkerOptions.CollisionBehavior.REQUIRED
) {
val mapApplier = currentComposer.applier as? MapApplier
val compositionContext = rememberCompositionContext()
ComposeNode<MarkerNode, MapApplier>(
factory = {
val advancedMarkerOptions = AdvancedMarkerOptions()
.position(state.position)
.collisionBehavior(collisionBehavior)
if (iconView != null) {
advancedMarkerOptions.iconView(iconView)
} else if (pinConfig != null) {
advancedMarkerOptions.icon(BitmapDescriptorFactory.fromPinConfig(pinConfig))
}
advancedMarkerOptions.contentDescription(contentDescription)
advancedMarkerOptions.alpha(alpha)
advancedMarkerOptions.anchor(anchor.x, anchor.y)
advancedMarkerOptions.draggable(draggable)
advancedMarkerOptions.flat(flat)
advancedMarkerOptions.infoWindowAnchor(infoWindowAnchor.x, infoWindowAnchor.y)
advancedMarkerOptions.position(state.position)
advancedMarkerOptions.rotation(rotation)
advancedMarkerOptions.snippet(snippet)
advancedMarkerOptions.title(title)
advancedMarkerOptions.visible(visible)
advancedMarkerOptions.zIndex(zIndex)
val marker = mapApplier?.map?.addMarker(advancedMarkerOptions)
?: error("Error adding marker")
marker.tag = tag
MarkerNode(
compositionContext = compositionContext,
marker = marker,
markerState = state,
onMarkerClick = onClick,
onInfoWindowClick = onInfoWindowClick,
onInfoWindowClose = onInfoWindowClose,
onInfoWindowLongClick = onInfoWindowLongClick,
infoContent = infoContent,
infoWindow = infoWindow,
)
},
update = {
update(onClick) { this.onMarkerClick = it }
update(onInfoWindowClick) { this.onInfoWindowClick = it }
update(onInfoWindowClose) { this.onInfoWindowClose = it }
update(onInfoWindowLongClick) { this.onInfoWindowLongClick = it }
update(infoContent) { this.infoContent = it }
update(infoWindow) { this.infoWindow = it }
update(alpha) { this.marker.alpha = it }
update(anchor) { this.marker.setAnchor(it.x, it.y) }
update(draggable) { this.marker.isDraggable = it }
update(flat) { this.marker.isFlat = it }
update(infoWindowAnchor) { this.marker.setInfoWindowAnchor(it.x, it.y) }
update(state.position) { this.marker.position = it }
update(rotation) { this.marker.rotation = it }
update(snippet) {
this.marker.snippet = it
if (this.marker.isInfoWindowShown) {
this.marker.showInfoWindow()
}
}
update(tag) { this.marker.tag = it }
update(title) {
this.marker.title = it
if (this.marker.isInfoWindowShown) {
this.marker.showInfoWindow()
}
}
update(pinConfig) {
if (iconView == null) {
this.marker.setIcon(pinConfig?.let { it1 ->
BitmapDescriptorFactory.fromPinConfig(
it1
)
})
}
}
update(visible) { this.marker.isVisible = it }
update(zIndex) { this.marker.zIndex = it }
}
)
}