That is a custom function I created to transform geojsons users get from an API into a feature layer.
I was actually able to utilize @mohannainar1 code to create a function that adds a datasource to a created featurelayer:
interface AddDataSourceLayerOptions {
layer: __esri.FeatureLayer;
jimuMapView: JimuMapView;
layerId: string;
dataSourcePrefix?: string;
zoomToLayer?: boolean;
layerIndex?: number;
label?: string;
}
async function addDataSourceLayerToMap({
layer,
jimuMapView,
layerId,
zoomToLayer = true,
layerIndex = 0,
label
}: AddDataSourceLayerOptions): Promise<string> {
const dataSourceManager = DataSourceManager.getInstance();
const data: DataSourceJson = {
id: layerId,
layerId: layer.id,
type: DataSourceTypes.FeatureLayer,
label: label || layer.title
};
const dataJson = Immutable(data);
const dataSourceOptions = {
id: `${layerId}_client`,
layer: layer,
dataSourceJson: dataJson
};
try {
const source = await dataSourceManager.createDataSource(dataSourceOptions);
jimuMapView.view.map.add(layer);
jimuMapView.createJimuLayerView(layer, jimuMapView.id, layerIndex, source, true);
if (zoomToLayer) {
await layer.when();
if (layer.fullExtent) {
await jimuMapView.view.goTo(layer.fullExtent);
}
}
return source.id;
} catch (error) {
console.error('Error creating DataSource:', error);
throw error;
}
}
// then to use it, I have a global variable called:
const layerIdCounters = new Map<string, number>();
function getUniqueLayerId(baseId: string): string {
const currentCount = layerIdCounters.get(baseId) || 0;
const newCount = currentCount + 1;
layerIdCounters.set(baseId, newCount);
return `${baseId}_${newCount}`;
}
const uniqueLayerId = getUniqueLayerId('parcels_layer');
console.log('uniqueLayerId:', uniqueLayerId)
const sourceId = await addDataSourceLayerToMap({
layer: api_feature_layer,
jimuMapView: jimuMapView,
layerId: uniqueLayerId,
label: 'parcels_api'
});