Create a custom dynamic entity data source and display it using a dynamic entity layer.
Use case
Developers can create a custom DynamicEntityDataSource
to be able to visualize data from a variety of different feeds as dynamic entities using a DynamicEntityLayer
. An example of this is in a mobile situational awareness app, where a custom DynamicEntityDataSource
can be used to connect to peer-to-peer feeds in order to visualize real-time location tracks from teammates in the field.
How to use the sample
Run the sample to view the map and the dynamic entity layer displaying the latest observation from the custom data source. Tap on a dynamic entity to view its attributes in LogCat.
How it works
Configure the custom data source:
- Create a custom data source using a
CustomDynamicEntityDataSource.EntityFeedProvider
. - Override
feed
with aSharedFlow<CustomDynamicEntityDataSource.FeedEvent>
. - Override
onLoad()
to specify theDynamicEntityDataSourceInfo
for a given unique entity ID field and a list ofField
objects matching the fields in the data source. - Override
OnConnect()
to begin asynchronously processing observations from the custom data source. - Loop through the observations and deserialize each observation into a
Point
object and aMap<String, Any?>
containing the attributes. - Emit an observation in the custom data source
feed
withCustomDynamicEntityDataSource.FeedEvent.NewObservation(point, attributes)
.
Configure the MapView:
- Create a
DynamicEntityLayer
using the custom data source implementation. - Update values in the layer's
trackDisplayProperties
to customize the layer's appearance. - Set up the layer's
labelDefinitions
to display labels for each dynamic entity. - Use
MapView.identify(...)
to display a dynamic entity's attributes in aCallout
.
Relevant API
- CustomDynamicEntityDataSource.EntityFeedProvider
- DynamicEntity
- DynamicEntityDataSource
- DynamicEntityLayer
- LabelDefinition
- TrackDisplayProperties
About the data
This sample uses a .json file containing observations of marine vessels in the Pacific North West hosted on ArcGIS Online.
Additional information
In this sample, we iterate through features in a GeoJSON file to mimic messages coming from a real-time feed. You can create a custom dynamic entity data source to process any data that contains observations which can be translated into map points (com.arcgismaps.geometry.Point
objects) with associated Map<String, Any?>
attributes.
This sample uses the GeoViewCompose Toolkit module to implement a Composable MapView, which supports the use of Callouts.
Tags
callout, data, dynamic, entity, flow, label, labeling, live, real-time, stream, track
Sample Code
/* Copyright 2024 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.arcgismaps.sample.addcustomdynamicentitydatasource
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
import com.esri.arcgismaps.sample.addcustomdynamicentitydatasource.screens.MainScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// authentication with an API key or named user is
// required to access basemaps and other location services
ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)
setContent {
SampleAppTheme {
SampleApp()
}
}
}
@Composable
private fun SampleApp() {
Surface(
color = MaterialTheme.colorScheme.background
) {
MainScreen(
sampleName = getString(R.string.app_name)
)
}
}
}