配置标记冲突行为
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以将 collisionBehavior
属性设置为以下值之一,以指定某个标记与其他标记或地图标签冲突时的行为方式:
REQUIRED
:无论是否发生冲突,都应始终显示标记。
REQUIRED_AND_HIDES_OPTIONAL
:无论是否发生冲突,都应始终显示标记。如果标记具有 OPTIONAL_AND_HIDES_LOWER_PRIORITY
碰撞行为,则与此标记重叠的默认地图标签将被隐藏。
OPTIONAL_AND_HIDES_LOWER_PRIORITY
:仅当标记不与其他标记重叠时才应显示。如果两个同属于这种类型的标记发生重叠,则会显示绘制顺序较高的那个标记。如果二者的绘制顺序相同,则会显示在屏幕上纵向位置较低的那个标记。
以下代码示例演示了每种碰撞行为值。如需使用此代码示例,请按照设置和向应用添加 3D 地图中的说明,使用基本 3D 地图设置 Android Studio 项目。然后,将以下代码添加到 MainActivity.kt
文件中:
// Add imports
import com.google.android.gms.maps3d.model.latLngAltitude
...
// Marker 1: REQUIRED
googleMap3D.addMarker(markerOptions {
position = latLngAltitude {
latitude = 52.52027645136134
longitude = 13.408271658592406
altitude = 0.0
}
label = "Collision Behavior: REQUIRED"
altitudeMode = AltitudeMode.CLAMP_TO_GROUND
isExtruded = true
isDrawnWhenOccluded = true
collisionBehavior = CollisionBehavior.REQUIRED
})
// Marker 2: REQUIRED_AND_HIDES_OPTIONAL
googleMap3D.addMarker(markerOptions {
position = latLngAltitude {
latitude = 52.519605780912585
longitude = 13.406867190588198
altitude = 150.0
}
label = "Colliding Behavior: REQUIRED_AND_HIDES_OPTIONAL"
altitudeMode = AltitudeMode.ABSOLUTE
isExtruded = true
isDrawnWhenOccluded = true
collisionBehavior = CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL
})
// Marker 3: OPTIONAL_AND_HIDES_LOWER_PRIORITY
googleMap3D.addMarker(markerOptions {
position = latLngAltitude {
latitude = 52.519882191069016
longitude = 13.407410777254293
altitude = 50.0
}
label = "Colliding Behavior: OPTIONAL_AND_HIDES_LOWER_PRIORITY"
altitudeMode = AltitudeMode.RELATIVE_TO_GROUND
isExtruded = true
isDrawnWhenOccluded = true
collisionBehavior = CollisionBehavior.OPTIONAL_AND_HIDES_LOWER_PRIORITY
})
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Configure marker collision behavior\n\nSelect platform: [Android](/maps/documentation/maps-3d/android-sdk/configure-marker-collision-behavior \"View this page for the Android platform docs.\") [iOS](/maps/documentation/maps-3d/ios-sdk/configure-marker-collision-behavior \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/3d/marker-collision \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\n| This product or feature is Experimental (pre-GA). Pre-GA products and features might have limited support, and changes to pre-GA products and features might not be compatible with other pre-GA versions. Pre-GA Offerings are covered by the [Google\n| Maps Platform Service Specific Terms](https://cloud.google.com/maps-platform/terms/maps-service-terms). For more information, see the [launch stage descriptions](/maps/launch-stages).\n\n\nYou can specify how a marker should behave when it collides with another\nmarker or map label by setting the\n[`collisionBehavior`](/maps/documentation/maps-3d/android-sdk/reference/com/google/android/gms/maps3d/model/CollisionBehavior)\nproperty to one of the following values:\n\n- `REQUIRED`: The marker should always be displayed regardless of collisions.\n- `REQUIRED_AND_HIDES_OPTIONAL`: The marker should always be displayed regardless of collisions. Markers with `OPTIONAL_AND_HIDES_LOWER_PRIORITY` collision behavior r the default map labels that overlap with this marker will be hidden.\n- `OPTIONAL_AND_HIDES_LOWER_PRIORITY`: The marker should only be displayed if it does not overlap with other markers. If two markers of this type would overlap, the one with the higher draw order is shown. If they have the same draw order, the one with the lower vertical screenposition is shown.\n\n\nThe following code\nsample demonstrates each of the collision behavior values. To use this code sample, follow the instructions in\n[Setup](/maps/documentation/maps-3d/android-sdk/setup) and\n[Add a 3D map to your app](/maps/documentation/maps-3d/android-sdk/add-a-3d-map) to set\nup your Android Studio project with a basic 3D map. Then, add the following code to the\n**`MainActivity.kt`** file: \n\n```kotlin\n// Add imports\nimport com.google.android.gms.maps3d.model.latLngAltitude\n\n...\n\n// Marker 1: REQUIRED\ngoogleMap3D.addMarker(markerOptions {\n position = latLngAltitude {\n latitude = 52.52027645136134\n longitude = 13.408271658592406\n altitude = 0.0\n }\n label = \"Collision Behavior: REQUIRED\"\n altitudeMode = AltitudeMode.CLAMP_TO_GROUND\n isExtruded = true\n isDrawnWhenOccluded = true\n collisionBehavior = CollisionBehavior.REQUIRED\n})\n\n// Marker 2: REQUIRED_AND_HIDES_OPTIONAL\ngoogleMap3D.addMarker(markerOptions {\n position = latLngAltitude {\n latitude = 52.519605780912585\n longitude = 13.406867190588198\n altitude = 150.0\n }\n label = \"Colliding Behavior: REQUIRED_AND_HIDES_OPTIONAL\"\n altitudeMode = AltitudeMode.ABSOLUTE\n isExtruded = true\n isDrawnWhenOccluded = true\n collisionBehavior = CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL\n})\n\n// Marker 3: OPTIONAL_AND_HIDES_LOWER_PRIORITY\ngoogleMap3D.addMarker(markerOptions {\n position = latLngAltitude {\n latitude = 52.519882191069016\n longitude = 13.407410777254293\n altitude = 50.0\n }\n label = \"Colliding Behavior: OPTIONAL_AND_HIDES_LOWER_PRIORITY\"\n altitudeMode = AltitudeMode.RELATIVE_TO_GROUND\n isExtruded = true\n isDrawnWhenOccluded = true\n collisionBehavior = CollisionBehavior.OPTIONAL_AND_HIDES_LOWER_PRIORITY\n})\n```"]]