I am developing an Android application using the ArcGIS Maps SDK for Kotlin (version 200.x) and I'm trying to load and display a GeoJSON file from local device storage.
I found the FeatureCollection.fromJsonOrNull to try to load the file.But it cannot be analyzed correctly.
fun loadGeoJSONFromContentUri(context:Context, uri: Uri, fileExtension: String) {
try {
val tempFile = File(context.cacheDir, "temp_${System.currentTimeMillis()}.$fileExtension")
context.contentResolver.openInputStream(uri)?.use { inputStream ->
FileOutputStream(tempFile).use { outputStream ->
inputStream.copyTo(outputStream)
}
}
val geoJsonString = tempFile.readText(Charsets.UTF_8)
val featureCollection = FeatureCollection.fromJsonOrNull(geoJsonString)
if(featureCollection == null){
tempFile.delete()
return
}
featureCollection.tables.forEach {
it.layer?.let { layer ->
arcGISMap.operationalLayers.add(layer)
}
}
tempFile.delete()
} catch (e: Exception) {
Toast.makeText(context, e.message ?: "GeoJSON file load faild", Toast.LENGTH_SHORT).show()
Log.e(TAG, " ${e.printStackTrace()}")
}
}Am I using the wrong method, or is there another recommended way to load a GeoJSON file?