|
| 1 | +/* Copyright 2025 Esri |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.arcgismaps.sample.stylepointwithdistancecompositescenesymbol.components |
| 18 | + |
| 19 | +import android.app.Application |
| 20 | +import androidx.lifecycle.AndroidViewModel |
| 21 | +import androidx.lifecycle.viewModelScope |
| 22 | +import com.arcgismaps.Color |
| 23 | +import com.arcgismaps.geometry.Point |
| 24 | +import com.arcgismaps.geometry.SpatialReference |
| 25 | +import com.arcgismaps.mapping.ArcGISScene |
| 26 | +import com.arcgismaps.mapping.ArcGISTiledElevationSource |
| 27 | +import com.arcgismaps.mapping.BasemapStyle |
| 28 | +import com.arcgismaps.mapping.Viewpoint |
| 29 | +import com.arcgismaps.mapping.view.GraphicsOverlay |
| 30 | +import com.arcgismaps.mapping.view.Graphic |
| 31 | +import com.arcgismaps.mapping.symbology.DistanceCompositeSceneSymbol |
| 32 | +import com.arcgismaps.mapping.symbology.DistanceSymbolRange |
| 33 | +import com.arcgismaps.mapping.symbology.ModelSceneSymbol |
| 34 | +import com.arcgismaps.mapping.symbology.SimpleMarkerSceneSymbol |
| 35 | +import com.arcgismaps.mapping.symbology.SimpleMarkerSymbol |
| 36 | +import com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyle |
| 37 | +import com.arcgismaps.mapping.view.Camera |
| 38 | +import com.arcgismaps.mapping.view.SurfacePlacement |
| 39 | +import com.arcgismaps.mapping.view.OrbitGeoElementCameraController |
| 40 | +import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy |
| 41 | +import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel |
| 42 | +import com.esri.arcgismaps.sample.stylepointwithdistancecompositescenesymbol.R |
| 43 | +import kotlinx.coroutines.launch |
| 44 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 45 | +import kotlinx.coroutines.flow.asStateFlow |
| 46 | +import java.io.File |
| 47 | + |
| 48 | +/** |
| 49 | + * ViewModel for the sample. Builds an ArcGISScene, a distance composite scene symbol and an orbit |
| 50 | + * camera controller. |
| 51 | + */ |
| 52 | +class StylePointWithDistanceCompositeSceneSymbolViewModel(app: Application) : |
| 53 | + AndroidViewModel(app) { |
| 54 | + |
| 55 | + // Lazy provision path for reference offline resources. |
| 56 | + private val provisionPath: String by lazy { |
| 57 | + app.getExternalFilesDir(null)?.path.toString() + |
| 58 | + File.separator + |
| 59 | + app.getString(R.string.style_point_with_distance_composite_scene_symbol_app_name) |
| 60 | + } |
| 61 | + |
| 62 | + // Construct the model file URI from the provision path. |
| 63 | + private val bristolModelUri |
| 64 | + get() = "$provisionPath${File.separator}Bristol.dae" |
| 65 | + |
| 66 | + // The model (3D) graphic target. |
| 67 | + private val planePosition = Point( |
| 68 | + x = -2.708, y = 56.096, z = 5000.0, |
| 69 | + spatialReference = SpatialReference.wgs84() |
| 70 | + ) |
| 71 | + |
| 72 | + // Distance composite symbol with three ranges (detailed model, simplified model, and a simple circle). |
| 73 | + private val distanceCompositeSymbol: DistanceCompositeSceneSymbol by lazy { |
| 74 | + // Close-up: Detailed 3D model. |
| 75 | + val planeModel = ModelSceneSymbol( |
| 76 | + uri = bristolModelUri, |
| 77 | + scale = 100.0F |
| 78 | + ) |
| 79 | + |
| 80 | + // Mid-distance: Simple cone symbol. |
| 81 | + val coneSymbol = SimpleMarkerSceneSymbol.cone( |
| 82 | + color = Color.red, |
| 83 | + diameter = 200.0, |
| 84 | + height = 600.0 |
| 85 | + ) |
| 86 | + |
| 87 | + // Far-distance: Simple 2D symbol. |
| 88 | + val circleSymbol = SimpleMarkerSymbol( |
| 89 | + style = SimpleMarkerSymbolStyle.Circle, |
| 90 | + color = Color.red, |
| 91 | + size = 10f |
| 92 | + ) |
| 93 | + |
| 94 | + DistanceCompositeSceneSymbol().apply { |
| 95 | + // Close-up: Detailed 3D model. |
| 96 | + ranges.add( |
| 97 | + DistanceSymbolRange( |
| 98 | + symbol = planeModel, |
| 99 | + minDistance = null, |
| 100 | + maxDistance = 10000.0 |
| 101 | + ) |
| 102 | + ) |
| 103 | + // Mid-distance: Simple cone symbol. |
| 104 | + ranges.add( |
| 105 | + DistanceSymbolRange( |
| 106 | + symbol = coneSymbol, |
| 107 | + minDistance = 10001.0, |
| 108 | + maxDistance = 30000.0 |
| 109 | + ) |
| 110 | + ) |
| 111 | + // Far-distance: Simple 2D symbol. |
| 112 | + ranges.add( |
| 113 | + DistanceSymbolRange( |
| 114 | + symbol = circleSymbol, |
| 115 | + minDistance = 30001.0, |
| 116 | + maxDistance = null |
| 117 | + ) |
| 118 | + ) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Graphic for the plane using the distance composite symbol. |
| 123 | + private val planeGraphic by lazy { |
| 124 | + Graphic( |
| 125 | + geometry = planePosition, |
| 126 | + symbol = distanceCompositeSymbol |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + // Orbit camera controller that targets the plane graphic. |
| 131 | + val orbitCameraController: OrbitGeoElementCameraController by lazy { |
| 132 | + OrbitGeoElementCameraController(planeGraphic, 4000.0).apply { |
| 133 | + setCameraPitchOffset(80.0) |
| 134 | + setCameraHeadingOffset(-30.0) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Scene using imagery basemap and a world elevation service. |
| 139 | + val arcGISScene = ArcGISScene(BasemapStyle.ArcGISImagery).apply { |
| 140 | + // Set the tiled elevation source |
| 141 | + baseSurface.elevationSources += ArcGISTiledElevationSource( |
| 142 | + uri = "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer" |
| 143 | + ) |
| 144 | + // Set the initial viewpoint |
| 145 | + val camera = Camera( |
| 146 | + latitude = 56.096, |
| 147 | + longitude = -2.708, |
| 148 | + altitude = 5000.0, |
| 149 | + heading = -30.0, |
| 150 | + pitch = 80.0, |
| 151 | + roll = 0.0 |
| 152 | + ) |
| 153 | + initialViewpoint = Viewpoint(boundingGeometry = camera.location, camera = camera) |
| 154 | + } |
| 155 | + |
| 156 | + // Graphics overlay to display the plane graphic using a distance composite symbol. |
| 157 | + private val graphicsOverlay by lazy { |
| 158 | + GraphicsOverlay(graphics = listOf(planeGraphic)).apply { |
| 159 | + sceneProperties.surfacePlacement = SurfacePlacement.Relative |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Expose the graphics overlay list for the SceneView |
| 164 | + val graphicsOverlays = listOf(graphicsOverlay) |
| 165 | + |
| 166 | + // SceneView proxy to hand to the composable SceneView |
| 167 | + val sceneViewProxy = SceneViewProxy() |
| 168 | + |
| 169 | + // Flow exposing the distance between camera and target (meters). |
| 170 | + private val _cameraDistanceMeters = MutableStateFlow(0.0) |
| 171 | + val cameraDistanceMeters = _cameraDistanceMeters.asStateFlow() |
| 172 | + |
| 173 | + // Message dialog VM to surface errors. |
| 174 | + val messageDialogVM = MessageDialogViewModel() |
| 175 | + |
| 176 | + init { |
| 177 | + // Load the scene and surface errors via the message dialog VM. |
| 178 | + viewModelScope.launch { |
| 179 | + arcGISScene.load().onFailure { messageDialogVM.showMessageDialog(it) } |
| 180 | + } |
| 181 | + |
| 182 | + // Collect the cameraDistance flow exposed by the controller |
| 183 | + viewModelScope.launch { |
| 184 | + // Update flow to keep UI reactive. |
| 185 | + orbitCameraController.cameraDistance.collect { distance -> |
| 186 | + _cameraDistanceMeters.value = distance |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments