Display dimension features from a mobile map package.
Use case
Dimensions show specific lengths or distances on a map. A dimension may indicate the length of a side of a building or land parcel, or the distance between two features, such as a fire hydrant and the corner of a building.
How to use the sample
When the sample loads, it will automatically display the map containing dimension features from the mobile map package. The name of the dimension layer containing the dimension features is displayed in the controls box. Control the visibility of the dimension layer with the "Settings" button, and apply a definition expression to show dimensions of greater than or equal to 450m in length using the "Definition Expression" checkbox.
How it works
- Create a
MobileMapPackage
specifying the path to the .mmpk file. - Load the mobile map package with
mobileMapPackage.loadAsync()
. - After it successfully loads, get the first map from the mmpk and set it to the map view:
mapView.map = mobileMapPackage.maps[0]
. - Loop through the map's layers to create a
DimensionLayer
. - Control the dimension layer's visibility with
dimensionLayer.isVisible
and set a definition expression withdimensionLayer.definitionExpression
.
Relevant API
- DimensionLayer
- MobileMapPackage
Offline data
- Download the data from ArcGIS Online.
- Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
- Execute the following commands:
adb push Edinburgh_Pylon_Dimensions.mmpk /Android/data/com.esri.arcgisruntime.sample.displaydimensions/files/Edinburgh_Pylon_Dimensions.mmpk
Link | Local Location |
---|---|
Edinburgh_Pylon_Dimensions.mmpk | /Android/data/com.esri.arcgisruntime.sample.displaydimensions/cache/Edinburgh_Pylon_Dimensions.mmpk |
About the data
This sample shows a subset of the Edinburgh, Scotland network of pylons, substations, and powerlines within an Edinburgh Pylon Dimensions mobile map package, digitized from satellite imagery. Note the data is intended as illustrative of the network only.
Additional information
Dimension layers can be taken offline from a feature service hosted on ArcGIS Enterprise 10.9 or later, using the GeodatabaseSyncTask. Dimension layers are also supported in mobile map packages or mobile geodatabases created in ArcGIS Pro 2.9 or later.
Tags
dimension, layer, mmpk, mobile map package, utility
Sample Code
/* Copyright 2022 Esri
*
* 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.esri.arcgisruntime.sample.displaydimensions
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.esri.arcgisruntime.layers.DimensionLayer
import com.esri.arcgisruntime.loadable.LoadStatus
import com.esri.arcgisruntime.mapping.MobileMapPackage
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.displaydimensions.databinding.ActivityMainBinding
import com.esri.arcgisruntime.sample.displaydimensions.databinding.DimensionsDialogLayoutBinding
class MainActivity : AppCompatActivity() {
private val TAG: String = MainActivity::class.java.simpleName
private lateinit var dimensionLayer: DimensionLayer
private var isDimensionLayerEnabled: Boolean = true
private var isDefinitionEnabled: Boolean = false
private val activityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
private val mapView: MapView by lazy {
activityMainBinding.mapView
}
private val settingsButton: ConstraintLayout by lazy {
activityMainBinding.settingsLayout
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activityMainBinding.root)
// create and load a mobile map package
val mobileMapPackage =
MobileMapPackage(getExternalFilesDir(null)?.path + "/Edinburgh_Pylon_Dimensions.mmpk")
mobileMapPackage.addDoneLoadingListener {
// check the mmpk has loaded successfully and that it contains a map
if (mobileMapPackage.loadStatus == LoadStatus.LOADED && mobileMapPackage.maps.isNotEmpty()) {
// add the map from the mobile map package to the map view, and set a min scale to maintain dimension readability
mapView.map = mobileMapPackage.maps[0]
mapView.map.minScale = 35000.0
// find the dimension layer within the map
dimensionLayer =
mapView.map.operationalLayers.firstOrNull { it is DimensionLayer } as DimensionLayer
} else {
val errorMessage =
"Failed to load the mobile map package: " + mobileMapPackage.loadError.message
Log.e(TAG, errorMessage)
Toast.makeText(
this,
errorMessage,
Toast.LENGTH_SHORT
).show()
}
}
mobileMapPackage.loadAsync()
settingsButton.setOnClickListener {
// inflate the dialog layout and get references to each of its components
val dialogBinding = DimensionsDialogLayoutBinding.inflate(LayoutInflater.from(this))
val dimensionLayerSwitch = dialogBinding.dimensionLayerSwitch.apply {
isChecked = isDimensionLayerEnabled
}
val definitionSwitch = dialogBinding.definitionSwitch.apply {
isChecked = isDefinitionEnabled
}
// set up the dialog
AlertDialog.Builder(this).apply {
setView(dialogBinding.root)
setTitle("Dimension options:")
dimensionLayerSwitch.setOnCheckedChangeListener { _, isEnabled ->
// set the visibility of the dimension layer
dimensionLayer.isVisible = isEnabled
isDimensionLayerEnabled = isEnabled
}
definitionSwitch.setOnCheckedChangeListener { _, isEnabled ->
// set a definition expression to show dimension lengths of
// greater than or equal to 450m when the checkbox is selected,
// or to reset the definition expression to show all
// dimension lengths when unselected
val defExpression =
if (isEnabled) "DIMLENGTH >= 450" else ""
dimensionLayer.definitionExpression = defExpression
isDefinitionEnabled = isEnabled
}
}.show()
}
}
override fun onPause() {
mapView.pause()
super.onPause()
}
override fun onResume() {
super.onResume()
mapView.resume()
}
override fun onDestroy() {
mapView.dispose()
super.onDestroy()
}
}