Select to view content in your preferred language

Kotlin Real Time Sample App

619
2
Jump to solution
10-02-2023 06:56 PM
LeighWoolley
Emerging Contributor

Hi,

Is there an equivalent Kotlin SDK sample app to the Java SDK:

add_custom_dynamic_entity_data_source
0 Kudos
1 Solution

Accepted Solutions
Shubham_Sharma
Esri Contributor

Hey @LeighWoolley,

The Add custom dynamic entity data source Kotlin sample is planned for the next SDK release, although the implementation should be fairly similar to Java. The API is supported in the Kotlin ArcGIS Maps SDK in version 200.2.0: API ref 

Configure the custom data source:

  • Create a custom data source implementation of a CustomDynamicEntityDataSource.
val dataSource = CustomDynamicEntityDataSource(
    FlowBuilderProvider(lifecycleScope, observationsFile)
)
  • Override onLoad() to specify the DynamicEntityDataSourceInfo for a given unique entity ID field and a list of Field objects matching the fields in the data source.
  • Override onConnect() to begin processing observations from the custom data source.
  • Loop through the observations and deserialize each observation into a Geometry object and a Map<String, Object> containing the attributes.
  • Use CustomDynamicEntityDataSource.FeedEvent.NewObservation to add each observation to the custom data source.
private class FlowBuilderProvider(
    scope: CoroutineScope,
    val observationsFile: File
) : CustomDynamicEntityDataSource.EntityFeedProvider {

    private val _feed: Flow<CustomDynamicEntityDataSource.FeedEvent> = flow {
        // set up the bufferedReader on the observationsFile
        // for each line of the file retrieve the geometry, field name and value for the observation
        emit(
            CustomDynamicEntityDataSource.FeedEvent.NewObservation(
                geometry,
                mapOf(trackIdField to trackIdValue)
            )
        )
    }
    override val feed: SharedFlow<CustomDynamicEntityDataSource.FeedEvent> =
        _feed.flowOn(Dispatchers.IO).shareIn(scope, SharingStarted.WhileSubscribed())

    override suspend fun onLoad(): DynamicEntityDataSourceInfo {
        return DynamicEntityDataSourceInfo(
            entityIdFieldName = trackIdField,
            fields = listOf(Field(FieldType.Int64, trackIdField, trackIdField))
        ).apply { spatialReference = SpatialReference.wgs84() }
    }

    override suspend fun onConnect() {}

    override suspend fun onDisconnect() {}

}

Configure the map view:

  • Create a DynamicEntityLayer using the custom data source implementation.
  • Update values in the layer's TrackDisplayProperties to customize the layer's appearance.
val layer = DynamicEntityLayer(dataSource).apply {
    trackDisplayProperties.showPreviousObservations = false
    trackDisplayProperties.showTrackLine = true
    trackDisplayProperties.maximumObservations = 40
}
operationalLayers.add(layer)

 

View solution in original post

2 Replies
Shubham_Sharma
Esri Contributor

Hey @LeighWoolley,

The Add custom dynamic entity data source Kotlin sample is planned for the next SDK release, although the implementation should be fairly similar to Java. The API is supported in the Kotlin ArcGIS Maps SDK in version 200.2.0: API ref 

Configure the custom data source:

  • Create a custom data source implementation of a CustomDynamicEntityDataSource.
val dataSource = CustomDynamicEntityDataSource(
    FlowBuilderProvider(lifecycleScope, observationsFile)
)
  • Override onLoad() to specify the DynamicEntityDataSourceInfo for a given unique entity ID field and a list of Field objects matching the fields in the data source.
  • Override onConnect() to begin processing observations from the custom data source.
  • Loop through the observations and deserialize each observation into a Geometry object and a Map<String, Object> containing the attributes.
  • Use CustomDynamicEntityDataSource.FeedEvent.NewObservation to add each observation to the custom data source.
private class FlowBuilderProvider(
    scope: CoroutineScope,
    val observationsFile: File
) : CustomDynamicEntityDataSource.EntityFeedProvider {

    private val _feed: Flow<CustomDynamicEntityDataSource.FeedEvent> = flow {
        // set up the bufferedReader on the observationsFile
        // for each line of the file retrieve the geometry, field name and value for the observation
        emit(
            CustomDynamicEntityDataSource.FeedEvent.NewObservation(
                geometry,
                mapOf(trackIdField to trackIdValue)
            )
        )
    }
    override val feed: SharedFlow<CustomDynamicEntityDataSource.FeedEvent> =
        _feed.flowOn(Dispatchers.IO).shareIn(scope, SharingStarted.WhileSubscribed())

    override suspend fun onLoad(): DynamicEntityDataSourceInfo {
        return DynamicEntityDataSourceInfo(
            entityIdFieldName = trackIdField,
            fields = listOf(Field(FieldType.Int64, trackIdField, trackIdField))
        ).apply { spatialReference = SpatialReference.wgs84() }
    }

    override suspend fun onConnect() {}

    override suspend fun onDisconnect() {}

}

Configure the map view:

  • Create a DynamicEntityLayer using the custom data source implementation.
  • Update values in the layer's TrackDisplayProperties to customize the layer's appearance.
val layer = DynamicEntityLayer(dataSource).apply {
    trackDisplayProperties.showPreviousObservations = false
    trackDisplayProperties.showTrackLine = true
    trackDisplayProperties.maximumObservations = 40
}
operationalLayers.add(layer)

 

LeighWoolley
Emerging Contributor

Great, thank you,

0 Kudos