以下代码示例演示了如何使用 CameraRestriction 来限制相机的地理边界以及相机的高度、航向和倾斜度值。如需使用此代码示例,请按照设置和向应用添加 3D 地图中的说明,使用基本 3D 地图设置 Android Studio 项目。
然后,将以下代码添加到 MainActivity.kt 文件中:
// Add imports and define constantsimportcom.google.android.gms.maps3d.model.AltitudeModeimportcom.google.android.gms.maps3d.model.LatLngAltitudeimportcom.google.android.gms.maps3d.model.cameraRestrictionimportcom.google.android.gms.maps3d.model.latLngAltitudeimportcom.google.android.gms.maps3d.model.latLngBoundsimportcom.google.android.gms.maps3d.model.polygonOptionsconstvalEMPIRE_STATE_BUILDING_LATITUDE=40.748233constvalEMPIRE_STATE_BUILDING_LONGITUDE=-73.985663privateconstvalNYC_SOUTH_WEST_LAT=40.68563088976172privateconstvalNYC_SOUTH_WEST_LNG=-74.05030430240065privateconstvalNYC_NORTH_EAST_LAT=40.85649214337128privateconstvalNYC_NORTH_EAST_LNG=-73.80240973771173privateconstvalMAX_ALTITUDE_NYC_METERS=10000.0privateconstvalMIN_ALTITUDE_NYC_METERS=500.0privatevalnycBounds=latLngBounds{northEastLat=NYC_NORTH_EAST_LATnorthEastLng=NYC_NORTH_EAST_LNGsouthWestLat=NYC_SOUTH_WEST_LATsouthWestLng=NYC_SOUTH_WEST_LNG}// Define the restrictionsvalnycCameraRestriction=cameraRestriction{minAltitude=MIN_ALTITUDE_NYC_METERSmaxAltitude=MAX_ALTITUDE_NYC_METERSminHeading=0.0maxHeading=360.0minTilt=0.0maxTilt=90.0bounds=nycBounds}
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Configure map and camera restrictions\n\nSelect platform: [Android](/maps/documentation/maps-3d/android-sdk/map-camera-restrictions \"View this page for the Android platform docs.\") [iOS](/maps/documentation/maps-3d/ios-sdk/map-camera-restrictions \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/3d/interaction \"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\nThe following code sample demonstrates how to use\n[`CameraRestriction`](/maps/documentation/maps-3d/android-sdk/reference/com/google/android/gms/maps3d/model/CameraRestriction)\nto restrict both the geographical boundaries of the camera and the values of\nthe camera's altitude, heading, and tilt. To use this code sample, follow\nthe instructions in\n[Setup](/maps/documentation/maps-3d/android-sdk/setup) and\n[Add a 3D map\nto your app](/maps/documentation/maps-3d/android-sdk/add-a-3d-map) to set up your Android Studio project with a basic 3D map.\nThen, add the following code to the\n**`MainActivity.kt`** file: \n\n```kotlin\n// Add imports and define constants\nimport com.google.android.gms.maps3d.model.AltitudeMode\nimport com.google.android.gms.maps3d.model.LatLngAltitude\nimport com.google.android.gms.maps3d.model.cameraRestriction\nimport com.google.android.gms.maps3d.model.latLngAltitude\nimport com.google.android.gms.maps3d.model.latLngBounds\nimport com.google.android.gms.maps3d.model.polygonOptions\n\nconst val EMPIRE_STATE_BUILDING_LATITUDE = 40.748233\nconst val EMPIRE_STATE_BUILDING_LONGITUDE = -73.985663\n\nprivate const val NYC_SOUTH_WEST_LAT = 40.68563088976172\nprivate const val NYC_SOUTH_WEST_LNG = -74.05030430240065\nprivate const val NYC_NORTH_EAST_LAT = 40.85649214337128\nprivate const val NYC_NORTH_EAST_LNG = -73.80240973771173\nprivate const val MAX_ALTITUDE_NYC_METERS = 10000.0\nprivate const val MIN_ALTITUDE_NYC_METERS = 500.0\n\nprivate val nycBounds = latLngBounds {\n northEastLat = NYC_NORTH_EAST_LAT\n northEastLng = NYC_NORTH_EAST_LNG\n southWestLat = NYC_SOUTH_WEST_LAT\n southWestLng = NYC_SOUTH_WEST_LNG\n}\n\n// Define the restrictions\nval nycCameraRestriction = cameraRestriction {\n minAltitude = MIN_ALTITUDE_NYC_METERS\n maxAltitude = MAX_ALTITUDE_NYC_METERS\n minHeading = 0.0\n maxHeading = 360.0\n minTilt = 0.0\n maxTilt = 90.0\n bounds = nycBounds\n}\n```\n| **Note:** When setting camera restrictions, you must set all of the following parameters: `maxAltitude`, `minAltitude`, `maxHeading`, `minHeading`, `maxTilt`, and `minTilt`; setting camera bounds is optional."]]