Select to view content in your preferred language

Android Studio throws "Val cannot be reassigned."

1440
2
Jump to solution
08-08-2023 08:55 AM
klebercj
Occasional Contributor

// create a map with the BasemapStyle topographic
val map = ArcGISMap(BasemapStyle.ArcGISImagery)

// Create an ArcGIS Map Image Layer for the ESRI map server layer
val mapServiceUrl = "https://.../MapServer"
val mapImageLayer = ArcGISMapImageLayer(mapServiceUrl)

// load sublayers of mapServiceUrl
val hospitalSublayer = ArcGISMapImageSublayer(1).apply {name = "Hospital"}

.......list of 35 sublayers

// create a group layer from scratch by adding the Sublayers as children
val mapSublayers = GroupLayer().apply {
name ="Group Base Map"
addAll(arrayListOf(, hospitalSublayer, ...35 sublayers listed out))
}

basemap.operationalLayers.add(mapImageLayer)
basemap.operationalLayers.add(mapSublayers)

 

I'm getting an error at "name ='Group Base Map'" where Android Studio throws "Val cannot be reassigned." Android Studio highlights "name" in the list block of 35 sub-layers where I load sublayers of mapServiceUrl.

 

0 Kudos
1 Solution

Accepted Solutions
PriyankaRupani
Esri Contributor

I tired the first code snippet to load the ArcGISMapImageLayer with its subLayers and I see the map being loaded successfully. I'm using the latest 200.2.0 SDK version, which will release in a few days.
Code I tried is below: 

// create and add a map with a navigation night basemap style
val map = ArcGISMap(BasemapStyle.ArcGISNavigationNight)
activityMainBinding.mapView.map = map

// create and add a map image layer to the map
val mapImageLayer = ArcGISMapImageLayer("URL")

// load sublayers of mapServiceUrl
val citiesSublayer = ArcGISMapImageSublayer(0).apply {name = "Cities"}
val highwaysSublayer = ArcGISMapImageSublayer(1).apply {name = "Highways"}
val statesSublayer = ArcGISMapImageSublayer(2).apply {name = "States"}
val countiesSublayer = ArcGISMapImageSublayer(3).apply {name = "Counties"}

// create a group layer from scratch by adding the Sublayers as children
val mapSublayers = GroupLayer().apply {
name ="Group Base Map"
addAll(arrayListOf(citiesSublayer,highwaysSublayer, statesSublayer,countiesSublayer ))
}

map.operationalLayers.add(mapImageLayer)
map.operationalLayers.add(mapSublayers)


I believe you are using 200.1.0 SDK version?

In 200.1.0 version, I see that "name" is a val. That is updated in 200.2.0 version to be a var. That's why it works in latter case.

To make it work in 200.1.0 version, you can remove the assignment to "name" altogether, and you should see the map layers load.
Something like this,

val mapSublayers = GroupLayer().apply {
Collections.addAll(
arrayListOf(
citiesSublayer,
highwaysSublayer,
statesSublayer,
countiesSublayer
)
)
}


Hope this was helpful.

View solution in original post

0 Kudos
2 Replies
klebercj
Occasional Contributor

To try and remove the error I rewrote to the following

// create a map with the BasemapStyle topographic
val map = ArcGISMap(BasemapStyle.ArcGISImagery)

// Create an ArcGIS Map Image Layer for the ESRI map server layer
val mapServiceUrl = "https://.../MapServer"
val mapImageLayer = ArcGISMapImageLayer(mapServiceUrl)

// load sublayers of mapServiceUrl
val sublayersWithNames = listOf(
"Hospital" to ArcGISMapImageSublayer(0),
.... list of 35 sublayers)

// create a group layer from scratch by adding the Sublayers as children
val mapSublayers = GroupLayer().apply {
name = "BaseMap Sublayers"
sublayersWithNames.forEach { (name, sublayer) ->
add(sublayer)
}
}

basemap.operationalLayers.add(mapImageLayer)
basemap.operationalLayers.add(mapSublayers)

 

Now "name" is not called out in any other function except the group layer.  But the error is still there  and now there is a second regarding add(sublayer)

 

Any help would be appreciated

0 Kudos
PriyankaRupani
Esri Contributor

I tired the first code snippet to load the ArcGISMapImageLayer with its subLayers and I see the map being loaded successfully. I'm using the latest 200.2.0 SDK version, which will release in a few days.
Code I tried is below: 

// create and add a map with a navigation night basemap style
val map = ArcGISMap(BasemapStyle.ArcGISNavigationNight)
activityMainBinding.mapView.map = map

// create and add a map image layer to the map
val mapImageLayer = ArcGISMapImageLayer("URL")

// load sublayers of mapServiceUrl
val citiesSublayer = ArcGISMapImageSublayer(0).apply {name = "Cities"}
val highwaysSublayer = ArcGISMapImageSublayer(1).apply {name = "Highways"}
val statesSublayer = ArcGISMapImageSublayer(2).apply {name = "States"}
val countiesSublayer = ArcGISMapImageSublayer(3).apply {name = "Counties"}

// create a group layer from scratch by adding the Sublayers as children
val mapSublayers = GroupLayer().apply {
name ="Group Base Map"
addAll(arrayListOf(citiesSublayer,highwaysSublayer, statesSublayer,countiesSublayer ))
}

map.operationalLayers.add(mapImageLayer)
map.operationalLayers.add(mapSublayers)


I believe you are using 200.1.0 SDK version?

In 200.1.0 version, I see that "name" is a val. That is updated in 200.2.0 version to be a var. That's why it works in latter case.

To make it work in 200.1.0 version, you can remove the assignment to "name" altogether, and you should see the map layers load.
Something like this,

val mapSublayers = GroupLayer().apply {
Collections.addAll(
arrayListOf(
citiesSublayer,
highwaysSublayer,
statesSublayer,
countiesSublayer
)
)
}


Hope this was helpful.

0 Kudos